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.
Well this seemed like a cool little challenge so I just had to try my hand at it.
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”
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.
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.
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.
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.