Converting Amount into Words using JavaScript in Adobe
2023-11-10 05:0:11 Author: blogs.sap.com(查看原文) 阅读量:10 收藏

Recently while working on a ByD project, user requirement is to print total amount into words according to Indian currency format. Because of some limitation we are not able to get that node into XML. That’s the reason, I had to write a code for conversion.

In this post I will provide insights on how to convert amount into words using JavaScript in Adobe forms.

Below is the list of content that I am going to cover:

1)Why I use JavaScript for converting amount into words

2)Syntax for accessing Context Data with JavaScript

3)JavaScript Program for conversion

—————————————————————————————————————-

1)Why I use JavaScript for converting amount into words:

       Following are the reasons for using JavaScript.

  • I have already tried Form Calc function ‘WordNum’ but this is working for English only.

         

 $.rawValue = WordNum(  FormPurchaseOrderChangeRequest.bdyMainPage1.frmSummaryBlock.amountinwords.TextField62.rawValue,2)

     

 

  •  And Formcalc is difficult scripting language for writing such code. It is the best choice when we work on functions like (date and time, arithmetic, logical etc.)

These are the reasons why I prefer JavaScript.

2)Syntax for accessing Context Data with JavaScript :

       Where should we have to write our Script in Adobe Forms:

           Menu Bar –> Pallets –> Script Editor

 

  • Normally, while writing code in form calc we can access any context data (Node) simply by their path.

                syntax: $.rawValue = <pathname>.rawValue

Example:

$.rawValue=FormPurchaseOrderChangeRequest.bdyMainPage1.frmAddrInfoBlock.maintable.Table3.HeaderRow.TextField[1].rawValue

  • For accessing context data using JavaScript, syntax is

                xfa.resolveNode(“xfa.dataset.data.<path.to.the.node>”)

Example:

              var curr = xfa.resolveNode (“$record.PurchaseOrder.Price.NetAmount”).value;

Here  $.record is my dataset , PurchaseOrder.Price.NetAmount.currencyCode is my path and          value is for getting their value.

3)JavaScript Program for converting Amount into Words according to Indian Currency Format:

var a = [”, ‘One ‘, ‘Two ‘, ‘Three ‘, ‘Four ‘, ‘Five ‘, ‘Six ‘, ‘Seven ‘, ‘Eight ‘, ‘Nine ‘, ‘Ten ‘, ‘Eleven ‘, ‘Twelve ‘, ‘Thirteen ‘,

‘Fourteen ‘, ‘Fifteen ‘, ‘Sixteen ‘, ‘Seventeen ‘, ‘Eighteen ‘, ‘Nineteen ‘];

var b = [”, ”, ‘Twenty’, ‘Thirty’, ‘Forty’, ‘Fifty’, ‘Sixty’, ‘Seventy’, ‘Eighty’, ‘Ninety’];

var curr = xfa.resolveNode (“$record.PurchaseOrder.Price.NetAmount.currencyCode”).value;

if(curr == “INR”){

function inWords(num1) {

var pos = num1.indexOf(“.”);

var num;

var dec;

if(pos == -1)

{

num = num1;

dec = “00”;

}

else

{

num = num1.substring(0, pos);

dec = num1.substring(pos +1, num1.length);

}

if ((num = num.toString()).length > 9) return ‘overflow’;

n = (‘000000000’ + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);

var str = ”;

var str1 = ‘Rupees ‘;

if (!n) return;

str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ‘ ‘ + a[n[1][1]]) + ‘Crore ‘ : ”;

str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ‘ ‘ + a[n[2][1]]) + ‘Lakh ‘ : ”;

str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ‘ ‘ + a[n[3][1]]) + ‘Thousand ‘ : ”;

str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ‘ ‘ + a[n[4][1]]) + ‘Hundred ‘ : ”;

//str += (n[5] != 0) ? ((str != ”) ? ‘and ‘ : ”) + (a[Number(n[5])] || b[n[5][0]] + ‘ ‘ + a[n[5][1]]) + ‘only ‘ : ”;

//str += (n[5] != 0) ? ((str != ”) ? ‘ ‘ : ”) + (a[Number(n[5])] || b[n[5][0]] + ‘ ‘ + a[n[5][1]]) + str1 : ” ;

str += (n[5] != 0) ? ((str != ”) ? ‘ ‘ : ”) + (a[Number(n[5])] || b[n[5][0]] + ‘ ‘ + a[n[5][1]]) + ‘Rupees’ : ‘Rupees ‘ ;

if ((dec = dec.toString()).length > 2) return ‘overflow’;

d = (’00’ + dec).substr(-2).match(/^(\d{2})$/);

if (!d) return;

str += ”;

str += (Number(d[1]) !== 0) ? ((str !== ”) ? ” and ” : ”) + (this.a[Number(d[1])] || this.b[d[1][0]] + ‘ ‘ + this.a[d[1][1]]) + ‘Paise Only’ : ‘Only’;

return str;

}

}

var num1 = xfa.resolveNode (“$record.PurchaseOrder.Price.GrossAmount”).value;

this.rawValue = inWords(num1.toString());

Output:

Hope the blog post is useful.

Regards,

Vrushali Chandalekar.


文章来源: https://blogs.sap.com/2023/11/09/converting-amount-into-words-using-javascript-in-adobe/
如有侵权请联系:admin#unsafe.sh