Transform a number in text

Hi,
is there any solutions in Glide for transforming a number (100) into a text (one hundred) ?
I asked ChatGPT to write me a code in Java Script and the result is this one :

function numberToWords(number) {
const units = [‘’, ‘unu’, ‘doi’, ‘trei’, ‘patru’, ‘cinci’, ‘sase’, ‘sapte’, ‘opt’, ‘noua’];
const tens = [‘’, ‘zece’, ‘douazeci’, ‘treizeci’, ‘patruzeci’, ‘cincizeci’, ‘saizeci’, ‘saptezeci’, ‘optzeci’, ‘nouazeci’];
const teens = [‘’, ‘unsprezece’, ‘doisprezece’, ‘treisprezece’, ‘paisprezece’, ‘cincisprezece’, ‘saisprezece’, ‘saptesprezece’, ‘optsprezece’, ‘nouasprezece’];
const thousands = [‘’, ‘mie’, ‘mii’, ‘milioane’, ‘miliarde’];

if (number == 0) {
return ‘zero’;
}

let words = ‘’;

if (number < 0) {
words += 'minus ';
number = Math.abs(number);
}

if (number < 10) {
words += units[number];
} else if (number < 20) {
words += teens[number - 10];
} else if (number < 100) {
words += tens[Math.floor(number / 10)];
if (number % 10 !== 0) {
words += ’ ’ + units[number % 10];
}
} else if (number < 1000) {
words += units[Math.floor(number / 100)] + ’ sute’;
if (number % 100 !== 0) {
words += ’ si ’ + numberToWords(number % 100);
}
} else {
let i = 0;
while (number >= 1000) {
number /= 1000;
i++;
}
words += numberToWords(Math.floor(number)) + ’ ’ + thousands[i];
if (number % 1 !== 0) {
words += ’ ’ + numberToWords(number % 1);
}
}

return words.trim();
}

I used Code column from Glide tables (with Java Script), I stored my number column into p1 variable and I replaced this p1 variable in Java Script code. The result is nothing :unamused:.
I don’t have any experience with JavaScript code (c’mon, I’m a Banker😜 ) and I need some guidance for this or a alternative solution for my problem.
Thank you,
M.V.

The parameters in the javascript column are always text strings. You need to convert it to a number.

Can you have decimals in your number? If so, you will need parseFloat(p1). If you do not have decimals, then you can use parseInt(p1). Either will convert the parameter into a numeric value that might work with the rest of your code.

Thank you, but am still confused. I have to read more (a lot) about JavaScript :unamused:

Wherever you are using the p1 parameter, wrap it with parseFloat() or parseInt() depending on if the number can contain decimals or not.

Instead of writing p1 in the code, you can write parseFloat(p1) for example.

If I have time later, I can try out the code and see if I can get it to work.

OK, just testing it very quickly, it does seem to work without having to convert the number to an Integer or Float. Are you actually calling the numberToWords function?

You may just need to use the function as you wrote it in your original post, but you have to actually call it in your code by adding the following to the very bottom. This will actually call the function and pass the p1 parameter into it.

return numberToWords(p1);

1 Like

Stupid me, i didn’t call the function :face_with_hand_over_mouth:
Now everything is Ok !!
Thank you, Jeff :+1:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.