Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

WordNum Decimal, 5 Decimal Places - not dollars?

Avatar

Level 3

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

1 Accepted Solution

Avatar

Correct answer by
Employee

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

 

 

Mayank_Tiwari_1-1640774297130.png

 

View solution in original post

5 Replies

Avatar

Employee

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

 

Avatar

Level 3

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?

Avatar

Employee

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.

 

Avatar

Level 3

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:

 

Natalieann75_0-1640219116488.png

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 !!

Avatar

Employee

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!

Avatar

Level 3

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 🙂

 

Natalieann75_0-1640745037634.png

*adding the brace (clearly in the wrong spot) gives the next error

Natalieann75_2-1640745483806.png

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. 

Natalieann75_4-1640746649277.png

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

 

 

Avatar

Employee

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;

Mayank_Tiwari_0-1640760985408.png

 

 

Avatar

Level 3

OK progress!!!  I am getting a result and it is noting the "point" but not the numbers after the decimal point - example below:

Natalieann75_0-1640765462492.png

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!!!

 

Avatar

Correct answer by
Employee

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

 

 

Mayank_Tiwari_1-1640774297130.png

 

Avatar

Level 3

You are AMAZIIIIINNNGGG!!!!

🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉

I cannot thank you enough - just the BEST.

Avatar

Level 3

Just for completeness for anyone else if they ever need it....every time I entered a 1 I got NaN

Natalieann75_0-1640830910528.png

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]+" ";

Natalieann75_1-1640831215317.png