Currency Number to Written Text

Looking for a method to write out a number as text. Such as with a check where 1,588.46 would become One Thousand Five Hundred Eighty-Eight and 46/100. I need English, French and Spanish.

I found this GitHub - yamadapc/js-written-number: Convert numbers to words - their written form but can’t see to get it to work in PlanetPress Connect.

Well this seemed like a cool little challenge so I just had to try my hand at it. :slight_smile:

I took the library you referred to and made some changes to make it work in the Designer (and also to add the handling of decimals, which the original library didn’t).

To use the functionality, you add the script as a control script in the Designer and then, from any standard script, you use the following syntax:

var result = numberToText(1234567.89, {lang:"en"} );
// returns "one million two hundred thirty-four thousand five hundred and sixty-eight and 89/100"

where the lang option can be either “en”, “es” or “fr

You can download the js code by clicking here.

Note that I have run some tests with the library and it seems to work fine, but if it breaks at any point, you’re on your own!

It looks good, but one item I’ve noticed.

My data has 3 decimals, but only want decimals. If the data type is a string I get this with 460/100. If I change it to currency the script will error out.

mille cinq cent quatre-vingt-huit et 460/100

var field, result = "";

field = record.fields["Payment_Amount"];
//if (field !== "") result += formatter.currencyNoSymbol(field);


var nresult = numberToText(field, {lang:"fr"} );
// returns "one million two hundred thirty-four thousand five hundred and sixty-eight and 89/100"

results.html(nresult);

I changed a couple of things in the file, so you should download it again. There’s now an option to specify the number of decimals and it also strips out any leading 0s in the decimal part (i.e.“... and 2/100” instead of “... and 02/100”).

Read the comments at the top of the file for syntax examples and default values.

When I added the updated code there was one minor error.

This now returns which appears to be correct.

mille cinq cent quatre-vingt-huit et 46/100
mil quinientos ochenta y ocho y 46/100
one thousand five hundred and eighty-eight and 46/100

versus what the previous version returned.

mille cinq cent quatre-vingt-huit et 460/100
mil quinientos ochenta y ocho y 460/100
one thousand five hundred and eighty-eight and 460/100

The is Great! Much Appreciated!

The function expects a number as a parameter, which is why it doesn’t use the parsefloat method (which converts a string to a float). While your changes are entirely kosher, a best practice would be to have the calling code do the conversion (e.g. numberToText(parseFloat(field)) ).

An even better alternative would be to change the function to handle any type of parameters:

function numberToText(n,options) {
if (typeof n == "undefined") {
n = 0;
} else if (typeof n == "string"){
var t = parseFloat(n);
n = isNaN(t) ? 0 : t;
}
// ...
}

I’ve never used isNaN before so will research it to understand the syntax and how it works. I understand that isNaN tests if the value is a number.

This is how I interpreted to add the additional code, which seems to work. I will test it further, but cannot until Tuesday afternoon or Wednesday morning.

Thanks again!

You were close. In JavaScript, NaN means “Not a number”. So the code first determines if the n parameter it received is a string. If it is, it attempts to convert it to a number using parseFloat() and stores the result in variable t. Then, with isNaN(t), the code checks if t is not a number, in other words, it checks if the conversion failed. If it did, it assigns 0 back to n, otherwise the converted number is stored in n.

I’m using this number to text script in another project but the script is rounding up.

Running this on 2021.1.1.6675 but the other was on 2020.1.
The other template works on 2021.1.1.6675.

I’ve copied the scripts Number2Text.txt (15.8 KB), Payment_English.txt (357 Bytes) and the new template still rounds the cents into the dollars, but retains the cents.

My data types are the same (XML), the field types are both strings, the only difference that I see is on the old data the amount has 3 digits after the decimal the new data only has 2 digits. I manually modified the data to have 3 digits, but it still rounded up.

I must be missing something, but can’t seem to fine it.

There’s a mistake in the writtenNumber() method of the NumberToText library.

The line n = Math.round(+n); should actually state n = Math.floor(+n);.

1 Like

Only checked a couple dozen examples, but this appears to have resolved the issue.