Question

Change Numbers in Words

Hi,

can you share any code or business process steps where I can convert

amounts in numerals to words? Ex if the amount is 1,00,000 rupees, the amount in words should be auto-calculated as "One Lakh rupees".

 

 

Like 0

Like

5 comments

Hello Bhumika,

 

Please use this part of code in the script task element and call the numToWords function for the parseInt value from the string you have on the page and then compose the result the numToWords function returns with the left part of the string:

 

 

const arr = x => Array.from(x);
const num = x => Number(x) || 0;
const str = x => String(x);
const isEmpty = xs => xs.length === 0;
const take = n => xs => xs.slice(0,n);
const drop = n => xs => xs.slice(n);
const reverse = xs => xs.slice(0).reverse();
const comp = f => g => x => f (g (x));
const not = x => !x;
const chunk = n => xs =>
  isEmpty(xs) ? [] : [take(n)(xs), ...chunk (n) (drop (n) (xs))];

let numToWords = n => {
  let a = [
    '', 'one', 'two', 'three', 'four',
    'five', 'six', 'seven', 'eight', 'nine',
    'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
    'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
  ];
  let b = [
    '', '', 'twenty', 'thirty', 'forty',
    'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
  ];
  let g = [
    '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
    'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'
  ];
  // this part is really nasty still
  // it might edit this again later to show how Monoids could fix this up
  let makeGroup = ([ones,tens,huns]) => {
    return [
      num(huns) === 0 ? '' : a[huns] + ' hundred ',
      num(ones) === 0 ? b[tens] : b[tens] && b[tens] + '-' || '',
      a[tens+ones] || a[ones]
    ].join('');
  };
  // "thousands" constructor; no real good names for this, i guess
  let thousand = (group,i) => group === '' ? group : ${group} ${g[i]};
  // execute !
  if (typeof n === 'number') return numToWords(String(n));
  if (n === '0')             return 'zero';
  return comp (chunk(3)) (reverse) (arr(n))
    .map(makeGroup)
    .map(thousand)
    .filter(comp(not)(isEmpty))
    .reverse()
    .join(' ');
};

 

Here is the result this function returns for 1201:

 

Please also note that you need to modify this function to include your custom names for numbers.

 

Best regards,

Oscar

Hy, please check that when I tried to write this code on my script task it shows a lot of errors and not working please find the screenshots and further assist me in this.

Hy Oscar give a look.

Bhumika Bisht,

 

This is an example of the JS code that works, but you need to develop the same one in terms of the Creatio Script task element. Please debug the code provided by us and create the same for the script task element. We don't have a ready example of this code for the script task element, but only JS code itself. Also you will need to modify it based on your conditions of numbers naming.

 

You can use this code also not as the script task, but as a separate function on the edit page schema and call it once some action is performed on the page, for example button is clicked.

 

Best regards,

Oscar

Hy can you tell me a part how will I add to my code when I need to convert for eg:1.23 one rupees twenty-three paisa or one point twenty-three. 

convertNumberToWords:function(){
        var amount=this.get("UsrAmount");
    var words = new Array(0);
    words[0] = "";
    words[1] = "One";
    words[2] = "Two";
    words[3] = "Three";
    words[4] = "Four";
    words[5] = "Five";
    words[6] = "Six";
    words[7] = "Seven";
    words[8] = "Eight";
    words[9] = "Nine";
    words[10] = "Ten";
    words[11] = "Eleven";
    words[12] = "Twelve";
    words[13] = "Thirteen";
    words[14] = "Fourteen";
    words[15] = "Fifteen";
    words[16] = "Sixteen";
    words[17] = "Seventeen";
    words[18] = "Eighteen";
    words[19] = "Nineteen";
    words[20] = "Twenty";
    words[30] = "Thirty'";
    words[40] = "Forty";
    words[50] = "Fifty";
    words[60] = "Sixty";
    words[70] = "Seventy";
    words[80] = "Eighty";
    words[90] = "Ninety";
    amount = amount.toString();
    var atemp = amount.split(",");
    var number = atemp[0].split(",").join("");
    var nlength = number.length;
    var wordsstring = "";
    if (nlength <= 9) {
        var narray = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var receivednarray = new Array(0);
        for (var i = 0; i < nlength; i++) {
            receivednarray[i] = number.substr(i, 1);
        }
        var j=0;
        for ( i = 9 - nlength, j = 0; i < 9; i++, j++) {
            narray[i] = receivednarray[j];
        }
         var sx = new Array(0);
        for ( i = 0, j = 1; i < 9; i++, j++) {
            if (i ===0 || i===2 || i===4 || i===7) {
                if (narray[i] === 1) {
                 narray[j] = 10 + parseInt(sx[j]);
                    narray[i] = 0;
                }
            }
        }
         var value = "";
        for ( i = 0; i < 9; i++) {
            if (i ===0 || i === 2 || i === 4 || i === 7) {
                value = narray[i] * 10;
                  this.set("UsrWord",value);
            } else {
                value = narray[i];
                this.set("UsrWord",value);
            }
            if (value !== 0) {
                wordsstring += words[value] + " ";
                this.set("UsrWord",wordsstring);
            }
            if ((i === 1 && value !==0) || (i=== 0 && value !== 0 && narray[i + 1] === 0)) {
                wordsstring += "Crores ";
                 this.set("UsrWord",wordsstring);
            }
            if ((i === 3 && value !== 0) || (i === 2 && value !== 0 && narray[i + 1] ===0)) {
                wordsstring += "Lakhs ";
                 this.set("UsrWord",wordsstring);
            }
            if ((i === 5 && value !== 0) || (i === 4 && value !==0 && narray[i + 1] === 0)) {
                wordsstring += "Thousand ";
                 this.set("UsrWord",wordsstring);
            }
            if (i ===6 && value !== 0 && (narray[i + 1] !== 0 && narray[i + 2] !== 0)) {
                wordsstring += "Hundred and ";
                 this.set("UsrWord",wordsstring);
            } else if (i === 6 && value !== 0) {
                wordsstring += "Hundred ";
                 this.set("UsrWord",wordsstring);
            }
        }
        wordsstring = wordsstring.split(",").join(" ");
         
    }
   
},

 

Show all comments