JavaScript numbers to Words

2019-01-01 01:06发布

I'm trying to convert numbers into english words, for example 1234 would become: "one thousand two hundred thirty four".

My Tactic goes like this:

  • Separate the digits to three and put them on Array (finlOutPut), from right to left.

  • Convert each group (each cell in the finlOutPut array) of three digits to a word (this what the triConvert function does). If all the three digits are zero convert them to "dontAddBigSuffix"

  • From Right to left, add thousand, million, billion, etc. If the finlOutPut cell equals "dontAddBigSufix" (because it was only zeroes), don't add the word and set the cell to " " (nothing).

It seems to work pretty well, but I've got some problems with numbers like 190000009, converted to: "one hundred ninety million". Somehow it "forgets" the last numbers when there are a few zeros.

What did I do wrong? Where is the bug? Why does it not work perfectly?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

<script type="text/javascript">
function update(){
    var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');

    var output = '';
    var numString =   document.getElementById('number').value;
    var finlOutPut = new Array();

    if (numString == '0') {
        document.getElementById('container').innerHTML = 'Zero';
        return;
    }

    if (numString == 0) {
        document.getElementById('container').innerHTML = 'messeg tell to enter numbers';
        return;
    }

    var i = numString.length;
    i = i - 1;

    //cut the number to grups of three digits and add them to the Arry
    while (numString.length > 3) {
        var triDig = new Array(3);
        triDig[2] = numString.charAt(numString.length - 1);
        triDig[1] = numString.charAt(numString.length - 2);
        triDig[0] = numString.charAt(numString.length - 3);

        var varToAdd = triDig[0] + triDig[1] + triDig[2];
        finlOutPut.push(varToAdd);
        i--;
        numString = numString.substring(0, numString.length - 3);
    }
    finlOutPut.push(numString);
    finlOutPut.reverse();

    //conver each grup of three digits to english word
    //if all digits are zero the triConvert
    //function return the string "dontAddBigSufix"
    for (j = 0; j < finlOutPut.length; j++) {
        finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
    }

    var bigScalCntr = 0; //this int mark the million billion trillion... Arry

    for (b = finlOutPut.length - 1; b >= 0; b--) {
        if (finlOutPut[b] != "dontAddBigSufix") {
            finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
            bigScalCntr++;
        }
        else {
            //replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
            finlOutPut[b] = ' ';
            bigScalCntr++; //advance the counter  
        }
    }

        //convert The output Arry to , more printable string 
        for(n = 0; n<finlOutPut.length; n++){
            output +=finlOutPut[n];
        }

    document.getElementById('container').innerHTML = output;//print the output
}

//simple function to convert from numbers to words from 1 to 999
function triConvert(num){
    var ones = new Array('', ' one', ' two', ' three', ' four', ' five', ' six', ' seven', ' eight', ' nine', ' ten', ' eleven', ' twelve', ' thirteen', ' fourteen', ' fifteen', ' sixteen', ' seventeen', ' eighteen', ' nineteen');
    var tens = new Array('', '', ' twenty', ' thirty', ' forty', ' fifty', ' sixty', ' seventy', ' eighty', ' ninety');
    var hundred = ' hundred';
    var output = '';
    var numString = num.toString();

    if (num == 0) {
        return 'dontAddBigSufix';
    }
    //the case of 10, 11, 12 ,13, .... 19 
    if (num < 20) {
        output = ones[num];
        return output;
    }

    //100 and more
    if (numString.length == 3) {
        output = ones[parseInt(numString.charAt(0))] + hundred;
        output += tens[parseInt(numString.charAt(1))];
        output += ones[parseInt(numString.charAt(2))];
        return output;
    }

    output += tens[parseInt(numString.charAt(0))];
    output += ones[parseInt(numString.charAt(1))];

    return output;
}   
</script>

</head>
<body>

<input type="text"
    id="number"
    size="70"
    onkeyup="update();"
    /*this code prevent non numeric letters*/ 
    onkeydown="return (event.ctrlKey || event.altKey 
                    || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
                    || (95<event.keyCode && event.keyCode<106)
                    || (event.keyCode==8) || (event.keyCode==9) 
                    || (event.keyCode>34 && event.keyCode<40) 
                    || (event.keyCode==46) )"/>
                    <br/>
<div id="container">Here The Numbers Printed</div>
</body>
</html>

21条回答
一个人的天荒地老
2楼-- · 2019-01-01 01:20

Here, I wrote an alternative solution:

1) The object containing the string constants:

var NUMBER2TEXT = {
    ones: ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
    tens: ['', '', 'twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'],
    sep: ['', ' thousand ', ' million ', ' billion ', ' trillion ', ' quadrillion ', ' quintillion ', ' sextillion ']
};

2) The actual code:

(function( ones, tens, sep ) {

    var input = document.getElementById( 'input' ),
        output = document.getElementById( 'output' );

    input.onkeyup = function() {
        var val = this.value,
            arr = [],
            str = '',
            i = 0;

        if ( val.length === 0 ) {
            output.textContent = 'Please type a number into the text-box.';
            return;  
        }

        val = parseInt( val, 10 );
        if ( isNaN( val ) ) {
            output.textContent = 'Invalid input.';
            return;   
        }

        while ( val ) {
            arr.push( val % 1000 );
            val = parseInt( val / 1000, 10 );   
        }

        while ( arr.length ) {
            str = (function( a ) {
                var x = Math.floor( a / 100 ),
                    y = Math.floor( a / 10 ) % 10,
                    z = a % 10;

                return ( x > 0 ? ones[x] + ' hundred ' : '' ) +
                       ( y >= 2 ? tens[y] + ' ' + ones[z] : ones[10*y + z] );
            })( arr.shift() ) + sep[i++] + str;
        }

        output.textContent = str;
    };

})( NUMBER2TEXT.ones, NUMBER2TEXT.tens, NUMBER2TEXT.sep );

Live demo: http://jsfiddle.net/j5kdG/

查看更多
ら面具成の殇う
3楼-- · 2019-01-01 01:21

Here's a solution that will handle any integer value that fit's in a string. I've defined number scales up to "decillion", so this solution should be accurate up to 999 decillion. After which you get things like "one thousand decillion" and so on.

JavaScript numbers start to fail around "999999999999999" so the convert function works with strings of numbers only.

Examples:

convert("365");
//=> "three hundred sixty-five"

convert("10000000000000000000000000000230001010109");
//=> "ten thousand decillion two hundred thirty billion one million ten thousand one hundred nine"

Code:

var lt20 = ["", "one", "two", "three", "four", "five", "six", "seven","eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ],
    tens = ["", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eightty", "ninety" ],
    scales = ["", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion" ],
    max = scales.length * 3;

function convert(val) {
    var len;

    // special cases
    if (val[0] === "-") { return "negative " + convert(val.slice(1)); }
    if (val === "0") { return "zero"; }

    val = trim_zeros(val);
    len = val.length;

    // general cases
    if (len < max) { return convert_lt_max(val); }
    if (len >= max) { return convert_max(val); }
}

function convert_max(val) {
    return split_rl(val, max)
        .map(function (val, i, arr) {
            if (i < arr.length - 1) {
                return convert_lt_max(val) + " " + scales.slice(-1);
            }
            return convert_lt_max(val);
        })
        .join(" ");
}       

function convert_lt_max(val) {
    var l = val.length;
    if (l < 4) {
        return convert_lt1000(val).trim();
    } else {
        return split_rl(val, 3)
            .map(convert_lt1000)
            .reverse()
            .map(with_scale)
            .reverse()
            .join(" ")
            .trim();
    }
}

function convert_lt1000(val) {
    var rem, l;

    val = trim_zeros(val);
    l = val.length;

    if (l === 0) { return ""; }
    if (l < 3) { return convert_lt100(val); }
    if (l === 3) { //less than 1000
        rem = val.slice(1);
        if (rem) {
            return lt20[val[0]] + " hundred " + convert_lt1000(rem);
        } else {
            return lt20[val[0]] + " hundred";
        }
    }
}

function convert_lt100(val) {
    if (is_lt20(val)) { // less than 20
        return lt20[val];
    } else if (val[1] === "0") {
        return tens[val[0]];
    } else {
        return tens[val[0]] + "-" +  lt20[val[1]];
    }
}


function split_rl(str, n) {
    // takes a string 'str' and an integer 'n'. Splits 'str' into
    // groups of 'n' chars and returns the result as an array. Works
    // from right to left.
    if (str) {
        return Array.prototype.concat
            .apply(split_rl(str.slice(0, (-n)), n), [str.slice(-n)]);
    } else {
        return [];
    }
}

function with_scale(str, i) {
    var scale;
    if (str && i > (-1)) {
        scale = scales[i];
        if (scale !== undefined) {
            return str.trim() + " " + scale;
        } else {
            return convert(str.trim());
        }
    } else {
        return "";
    }
}

function trim_zeros(val) {
    return val.replace(/^0*/, "");
}

function is_lt20(val) {
    return parseInt(val, 10) < 20;
}
查看更多
情到深处是孤独
4楼-- · 2019-01-01 01:22

I would like to point out that the original logic fails for values between x11-x19, where x >= 1. For example, 118 returns "one hundred eight". This is because these numbers are processed by the following code in triConvert():

//100 and more
if (numString.length == 3) {
    output = ones[parseInt(numString.charAt(0))] + hundred;
    output += tens[parseInt(numString.charAt(1))];
    output += ones[parseInt(numString.charAt(2))];
    return output;
}

here, the character representing the tens digit is used to index the tens[] array, which has an empty string at index [1], so 118 become 108 in effect.

It might be better to deal with the hundreds (if any first), then run the ones and tens through the same logic. Instead of:

//the case of 10, 11, 12 ,13, .... 19 
if (num < 20) {
    output = ones[num];
    return output;
}

//100 and more
if (numString.length == 3) {
    output = ones[parseInt(numString.charAt(0))] + hundred;
    output += tens[parseInt(numString.charAt(1))];
    output += ones[parseInt(numString.charAt(2))];
    return output;
}

output += tens[parseInt(numString.charAt(0))];
output += ones[parseInt(numString.charAt(1))];

return output;

I would suggest:

// 100 and more
if ( numString.length == 3 ) 
 {
   output  = hundreds[ parseInt( numString.charAt(0) ) ] + hundred ;
   num = num % 100 ;
   numString = num.toString() ;
 }

if ( num < 20 )  
 {
   output += ones[num] ;
 }
else 
 { // 20-99 
   output += tens[ parseInt( numString.charAt(0) ) ] ;
   output += '-' + ones[ parseInt( numString.charAt(1) ) ] ;  
 }

 return output;

It seems to me that the suggested code is both shorter and clearer, but I might be biased ;-)

查看更多
何处买醉
5楼-- · 2019-01-01 01:23

If anybody ever wants to do this but in Spanish (en español), here's my code based on Hardik's

function num2str(num, moneda) {
    moneda = moneda || (num !== 1 ? "pesos" : "peso");
    var fraction = Math.round(__cf_frac(num) * 100);
    var f_text = " (" + pad(fraction, 2) + "/100 M.N.)";


    return __cf_convert_number(num) + " " + moneda + f_text;
}

function __cf_frac(f) {
    return f % 1;
}

function __cf_convert_number(number) {
    if ((number < 0) || (number > 999999999)) {
        throw Error("N\u00famero fuera de rango");
    }
    var millon = Math.floor(number / 1000000);
    number -= millon * 1000000;
    var cientosDeMiles = Math.floor(number / 100000);
    number -= cientosDeMiles * 100000;
    var miles = Math.floor(number / 1000);
    number -= miles * 1000;
    var centenas = Math.floor(number / 100);
    number = number % 100;
    var tn = Math.floor(number / 10);
    var one = Math.floor(number % 10);
    var res = "";

    var cientos = Array("", "cien", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos");
    if (millon > 0) {
        res += (__cf_convert_number(millon) + (millon === 1 ? " mill\u00f3n" : " millones"));
    }
    if (cientosDeMiles > 0) {
        res += (((res == "") ? "" : " ") +
            cientos[cientosDeMiles] + (miles > 0 || centenas > 0 || tn > 0 || one < 0 ? (cientosDeMiles == 1 ? "to " : " ") : ""));
    }
    if (miles > 0) {
        res += (((res == "") ? "" : " ") +
            __cf_convert_number(miles) + " mil");
    }
    if (centenas) {
        res += (((res == "") ? "" : " ") +
            cientos[centenas] + (tn > 0 || one > 0 ? (centenas > 1 ? " " : "to ") : ""));
    }


    var ones = Array("", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve");
    var tens = Array("", "", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa");

    if (tn > 0 || one > 0) {
        if (tn < 2) {
            res += ones[tn * 10 + one];
        }
        else {
            if (tn === 2 && one > 0)
                res += "veinti" + ones[one];
            else {
                res += tens[tn];
                if (one > 0) {
                    res += (" y " + ones[one]);
                }
            }
        }
    }

    if (res == "") {
        res = "cero";
    }
    return res.replace("  ", " ");
}

function pad(num, largo, char) {
    char = char || '0';
    num = num + '';
    return num.length >= largo ? num : new Array(largo - num.length + 1).join(char) + num;
}

Result:

num2str(123456789)
"ciento veintitres millones cuatrocientos cincuenta y seis mil setecientos ochenta y nueve pesos (00/100 M.N.)"
查看更多
时光乱了年华
6楼-- · 2019-01-01 01:24

Indian Version

Updated version of @jasonhao 's answer for Indian currency

    function intToEnglish(number){

    var NS = [
        {value: 10000000, str: "Cror"},
        {value: 100000, str: "Lakhs"},
        {value: 1000, str: "thousand"},
        {value: 100, str: "hundred"},
        {value: 90, str: "ninety"},
        {value: 80, str: "eighty"},
        {value: 70, str: "seventy"},
        {value: 60, str: "sixty"},
        {value: 50, str: "fifty"},
        {value: 40, str: "forty"},
        {value: 30, str: "thirty"},
        {value: 20, str: "twenty"},
        {value: 19, str: "nineteen"},
        {value: 18, str: "eighteen"},
        {value: 17, str: "seventeen"},
        {value: 16, str: "sixteen"},
        {value: 15, str: "fifteen"},
        {value: 14, str: "fourteen"},
        {value: 13, str: "thirteen"},
        {value: 12, str: "twelve"},
        {value: 11, str: "eleven"},
        {value: 10, str: "ten"},
        {value: 9, str: "nine"},
        {value: 8, str: "eight"},
        {value: 7, str: "seven"},
        {value: 6, str: "six"},
        {value: 5, str: "five"},
        {value: 4, str: "four"},
        {value: 3, str: "three"},
        {value: 2, str: "two"},
        {value: 1, str: "one"}
      ];

      var result = '';
      for (var n of NS) {
        if(number>=n.value){
          if(number<=90){
            result += n.str;
            number -= n.value;
            if(number>0) result += ' ';
          }else{
            var t =  Math.floor(number / n.value);
            console.log(t);
            var d = number % n.value;
            if(d>0){
              return intToEnglish(t) + ' ' + n.str +' ' + intToEnglish(d);
            }else{
              return intToEnglish(t) + ' ' + n.str;
            }

          }
        }
      }
      return result;
    }
查看更多
怪性笑人.
7楼-- · 2019-01-01 01:24

Source: http://javascript.about.com/library/bltoword.htm Smallest script that I found:

  <script type="text/javascript" src="toword.js">
    var words = toWords(12345);
    console.log(words);
  </script>

Enjoy!

查看更多
登录 后发表回答