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
  • 2502 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 22, 2021

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.