WordNum Decimal, 5 Decimal Places - not dollars? | Community
Skip to main content
Level 2
December 21, 2021
Solved

WordNum Decimal, 5 Decimal Places - not dollars?

  • December 21, 2021
  • 1 reply
  • 2508 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mayank_Tiwari

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;}

 

 

 

1 reply

Mayank_Tiwari
Adobe Employee
Adobe Employee
December 21, 2021

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,' ');}

 

Level 2
December 21, 2021

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?

Mayank_Tiwari
Adobe Employee
Adobe Employee
December 29, 2021

Still no good 😞  Shifts the error up to line 7 telling me no closing brace.  If I add a closing brace to Line 7, it just shifts down to the next error being an illegal "return" which says to me I have the closing brace in the wrong spot - but I have no idea where it should go 🙂

 

*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;