Hello - I am sure this is easy, but I can't seem to find anything not related to $ and ¢. I wish to convert a number with 5 decimal places to words - but I can't seem to find how. It is not dollars - they are units from a managed fund - so 125.62365 for example. Is this possible?
WordNum works well - but I am sure just don't know how to extend it...
Nat
Solved! Go to Solution.
Views
Replies
Total Likes
Replace the function with the one below. It would work then as expected.
function toWords(s){s = s.toString();
s =s.replace(/[\, ]/g,'');
if (s != parseFloat(s)) return 'not a number';
var x =s.indexOf('.');
if (x == -1) x = s.length;
if (x > 15) return 'too big';
var n = s.split(''); var str = ''; var sk = 0;
for (var i=0; i < x; i++) {
if((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}}
else if (n[i]!=0) {str +=dg[n[i]] +' ';
if ((x-i)%3==0) str += 'hundred ';sk=1;}
if ((x-i)%3==1) {if (sk)
str += th[(x-i-1)/3]+" ";sk=0;}}
if (x != s.length) {var y = s.length;
str = str.substring(0, str.length - 1);
str +="point ";
for (var i=x+1; i<y;i++)str.replace(/\s+/g,' ');}
var deci = s.split(".")[1];
var deciword = "";
for(var i=0;i<deci.length;i++)
if(deci[i]=="0")deciword=deciword+dg[0]+" ";
else if(deci[i]=="1")deciword+=dg[1]+" ";
else if(deci[i]=="2")deciword+=dg[2]+" ";
else if(deci[i]=="3")deciword+=dg[3]+" ";
else if(deci[i]=="4")deciword+=dg[4]+" ";
else if(deci[i]=="5")deciword+=dg[5]+" ";
else if(deci[i]=="6")deciword+=dg[6]+" ";
else if(deci[i]=="7")deciword+=dg[7]+" ";
else if(deci[i]=="8")deciword+=dg[8]+" ";
else if(deci[i]=="9")deciword+=dg[9]+" ";
return str+deciword;}
You may achieve this by JavaScript, as explained here.
Code:
// used exactly as shown (you can change the numbering system if you wish) // American Numbering System var th = ['','thousand','million', 'billion','trillion']; // uncomment this line for English Number System // var th = ['','thousand','million', 'milliard','billion']; var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s =s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x =s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n =s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str +=dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; istr.replace(/\s+/g,' ');}
Thank you for this! I think I had seen this in my hunt for answers but did not think it applied to AEM. Would you be able to help me with where this goes? The article states it is for a web page and to "put it in the head of the page". I am not a developer - I get by with the basics, but this one is a bit beyond me!
Which action would this sit within?
Are you trying to achive this in Adaptive Form or in AEM Forms Desginer? If you are using Designer, then you would need to create a Script object which would contain the toWords function. Then you would need to call this function from the Form's script. To know how to work with Script objects, please refer to this link.
Thank you so much for your time. I think this one it a bit above my skillset. If I copy and paste the above into a Script Object, I have an error:
Basically, on the exit event of a number field (numFigures) I have used WordNum to calculate a written result in a field txtWords . So I guess I should reference the SO in the exit event of the numFigures, but I don't think I am going to be able to work out how. However - I will make this as correct for anyone else who is better at this stuff than I am and may find it useful.
Really appreciate your time @Mayank_Tiwari !!
Please replace:
for (var i=x+1; istr.replace(/\s+/g,' ');
with the following:
for(var i=x+1; i<y;i++)
str.replace(/\s+/g,' ');
It should work then. Thanks for pointing out the syntax error!
Still no good
*adding the brace (clearly in the wrong spot) gives the next error
I have tried to apply some logic and have added it here - and the error has cleared - but again, I have no idea if it is correct.
I have tried to research how to reference this to kick it off but I think beyond me. If the script object is called soDecimal, how do I reference this on the exit of my numFigure? The only thing I can find that makes any sense is from the page you directed me to, but even using the examples on this page I cannot get a result....
script_object . function_name ( parameter1 ,...);
which I think is:
soDecimal.toWords (but what goes here, ...); if WordNum is a formcalc command?
Nat
I am assuming you have a textfield object with the name numFigure in your form . Then you may call the function from the exit event of numFigure like this:
numFigure.rawValue = soDecimal.toWords(numFigure.rawValue);
Also, toWords method should return the method output. So, you need to add the following line as the last line of the method.
return str;
OK progress!!! I am getting a result and it is noting the "point" but not the numbers after the decimal point - example below:
No error showing on the debugger when I try it in Acrobat - could it be an adjustment in the actual code?
@Mayank_Tiwari - you are some kind of legend to get me this far sir!!!
Replace the function with the one below. It would work then as expected.
function toWords(s){s = s.toString();
s =s.replace(/[\, ]/g,'');
if (s != parseFloat(s)) return 'not a number';
var x =s.indexOf('.');
if (x == -1) x = s.length;
if (x > 15) return 'too big';
var n = s.split(''); var str = ''; var sk = 0;
for (var i=0; i < x; i++) {
if((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}}
else if (n[i]!=0) {str +=dg[n[i]] +' ';
if ((x-i)%3==0) str += 'hundred ';sk=1;}
if ((x-i)%3==1) {if (sk)
str += th[(x-i-1)/3]+" ";sk=0;}}
if (x != s.length) {var y = s.length;
str = str.substring(0, str.length - 1);
str +="point ";
for (var i=x+1; i<y;i++)str.replace(/\s+/g,' ');}
var deci = s.split(".")[1];
var deciword = "";
for(var i=0;i<deci.length;i++)
if(deci[i]=="0")deciword=deciword+dg[0]+" ";
else if(deci[i]=="1")deciword+=dg[1]+" ";
else if(deci[i]=="2")deciword+=dg[2]+" ";
else if(deci[i]=="3")deciword+=dg[3]+" ";
else if(deci[i]=="4")deciword+=dg[4]+" ";
else if(deci[i]=="5")deciword+=dg[5]+" ";
else if(deci[i]=="6")deciword+=dg[6]+" ";
else if(deci[i]=="7")deciword+=dg[7]+" ";
else if(deci[i]=="8")deciword+=dg[8]+" ";
else if(deci[i]=="9")deciword+=dg[9]+" ";
return str+deciword;}
You are AMAZIIIIINNNGGG!!!!
I cannot thank you enough - just the BEST.
Just for completeness for anyone else if they ever need it....every time I entered a 1 I got NaN
I found this on the "1" line in the script code and removed it the extra plus sign after the = sign.....and now it works beautifully!!
else if(deci[i]=="1")deciword+=+dg[1]+" ";
Views
Likes
Replies
Views
Likes
Replies