Forum Academy Marketplace Showcase Pricing Features

Convert an amount to a letter

convert an amount to a letter.
example:
120 $ in one hundred and twenty

there isn’t a plugin for this, however you may use the toolbox plugin and the following javascript library/snippet

source: https://codepen.io/saqib1144/pen/vbGEyd

The Code

function numToText(numberInput){
    

    let oneToTwenty = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ',
    'eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
    let tenth = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

    if(numberInput.toString().length > 7) return myDiv.innerHTML = 'overlimit' ;
    console.log(numberInput);
    //let num = ('0000000000'+ numberInput).slice(-10).match(/^(\d{1})(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
  let num = ('0000000'+ numberInput).slice(-7).match(/^(\d{1})(\d{1})(\d{2})(\d{1})(\d{2})$/);
    console.log(num);
    if(!num) return;

    let outputText = num[1] != 0 ? (oneToTwenty[Number(num[1])] || `${tenth[num[1][0]]} ${oneToTwenty[num[1][1]]}` )+' million ' : ''; 
  
    outputText +=num[2] != 0 ? (oneToTwenty[Number(num[2])] || `${tenth[num[2][0]]} ${oneToTwenty[num[2][1]]}` )+'hundred ' : ''; 
    outputText +=num[3] != 0 ? (oneToTwenty[Number(num[3])] || `${tenth[num[3][0]]} ${oneToTwenty[num[3][1]]}`)+' thousand ' : ''; 
    outputText +=num[4] != 0 ? (oneToTwenty[Number(num[4])] || `${tenth[num[4][0]]} ${oneToTwenty[num[4][1]]}`) +'hundred ': ''; 
    outputText +=num[5] != 0 ? (oneToTwenty[Number(num[5])] || `${tenth[num[5][0]]} ${oneToTwenty[num[5][1]]} `) : ''; 

    return outputText;
}

Example

input output
numToText(120) one hundred and twenty
numToText(9999999) nine million nine hundred ninety nine thousand nine hundred ninety nine
numToText(10000000) overlimit

Bubble Setup: Client-side

  • place javascript to bubble element on the page
  • give the element a function name something like bubble_fn_num_text; set as output type text
  • in page workflow run javascript; insert code ^^ block above and this line bubble_fn_num_text(numToText(<your numeral to be converted>))
  • read the text output value from your JStoBubble element into a page element or process with a subsequent workflow

Overall this is a reasonable way to convert a number (numeral) to a string (text) value in sentence form.

4 Likes

Merci