A simple example of converting an JS number to an amount in words

  • 2021-07-09 06:37:44
  • OfStack

A Simple Example of Converting JS Numbers to Capital Amount


function DX(n) {

    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
      return " Illegal data ";
    var unit = " Thousands of billions, thousands of billions, thousands of dollars and cents ", str = "";
      n += "00";
    var p = n.indexOf('.');
    if (p >= 0)
      n = n.substring(0, p) + n.substr(p+1, 2);
      unit = unit.substr(unit.length - n.length);
    for (var i=0; i < n.length; i++)
      str += ' Zero one two three four five six seven eight nine '.charAt(n.charAt(i)) + unit.charAt(i);
    return str.replace(/ Zero ( Thousands | Hundred | Pick up | Angle )/g, " Zero ").replace(/( Zero )+/g, " Zero ").replace(/ Zero ( Ten thousand | Billion | Yuan )/g, "$1").replace(/( Billion ) Ten thousand | One ( Pick up )/g, "$1$2").replace(/^ Metadata zero ?| Zero points /g, "").replace(/ Yuan $/g, " Element integral ");
}

Related articles: