Convert digits into words with JavaScript

后端 未结 27 1477
我寻月下人不归
我寻月下人不归 2020-11-22 15:08

I am making a code which converts the given amount into words, heres is what I have got after googling. But I think its a little lengthy code to achieve a simple task. Two R

27条回答
  •  爱一瞬间的悲伤
    2020-11-22 15:53

    Converting the input string into a number rather than keeping it as a string, limits the solution to the maximum allowed float / integer value on that machine/browser. My script below handles currency up to 1 Trillion dollars - 1 cent :-). I can be extended to handle up to 999 Trillions by adding 3 or 4 lines of code.

    var ones = ["","One","Two","Three","Four","Five","Six","Seven","Eight",
                "Nine","Ten","Eleven","Twelve","Thirteen","Fourteen",
                "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]; 
    
    var tens = ["","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy",
                "Eighty","Ninety"]; 
    
    
    function words999(n999)   {    // n999 is an integer less than or equal to 999.
    //
    // Accept any 3 digit int incl 000 & 999 and return words.
    // 
    
        var words = ''; var Hn = 0; var n99 = 0;
    
        Hn = Math.floor(n999 / 100);                  // # of hundreds in it
    
        if (Hn > 0)   {                               // if at least one 100
    
          words = words99(Hn) + " Hundred";           // one call for hundreds
        }
    
        n99 = n999 - (Hn * 100);                      // subtract the hundreds.
    
        words += ((words == '')?'':' ') + words99(n99); // combine the hundreds with tens & ones.
    
        return words;
    }                            // function words999( n999 )
    
    function words99(n99)   {    // n99 is an integer less than or equal to 99.
    //
    // Accept any 2 digit int incl 00 & 99 and return words.
    // 
    
        var words = ''; var Dn = 0; var Un = 0;
    
        Dn = Math.floor(n99 / 10);           // # of tens
    
        Un = n99 % 10;                       // units
    
        if (Dn > 0 || Un > 0) {
    
          if (Dn < 2) {
    
            words += ones[Dn * 10 + Un];     // words for a # < 20
    
          } else {
    
            words += tens[Dn];
    
            if (Un > 0) words += "-" + ones[Un];
          }
        }                               // if ( Dn > 0 || Un > 0 )
    
        return words;
    }                                   // function words99( n99 )
    
    function getAmtInWords(id1, id2) {  // use numeric value of id1 to populate text in id2 
    //
    // Read numeric amount field and convert into word amount
    // 
    
        var t1 = document.getElementById(id1).value;
    
        var t2 = t1.trim();
    
        amtStr = t2.replace(/,/g,'');        // $123,456,789.12 = 123456789.12
    
        dotPos = amtStr.indexOf('.');        // position of dot before cents, -ve if it doesn't exist.
    
        if (dotPos > 0) {
    
          dollars = amtStr.slice(0,dotPos);  // 1234.56 = 1234
          cents   = amtStr.slice(dotPos+1);  // 1234.56 = .56
    
        } else if (dotPos == 0) {
    
          dollars = '0';
          cents   = amtStr.slice(dotPos+1);  // 1234.56 = .56
    
        } else {
    
          dollars = amtStr.slice(0);         // 1234 = 1234
          cents   = '0'; 
        }
    
        t1      = '000000000000' + dollars;  // to extend to trillion, use 15 zeros
        dollars =  t1.slice(-12);            // and -15 here.
    
        billions  = Number(dollars.substr(0,3));
        millions  = Number(dollars.substr(3,3));
        thousands = Number(dollars.substr(6,3));
        hundreds  = Number(dollars.substr(9,3));
    
        t1 = words999(billions);    bW = t1.trim();   // Billions  in words
    
        t1 = words999(millions);    mW = t1.trim();   // Millions  in words
    
        t1 = words999(thousands);   tW = t1.trim();   // Thousands in words
    
        t1 = words999(hundreds);    hW = t1.trim();   // Hundreds  in words
    
        t1 = words99(cents);        cW = t1.trim();   // Cents     in words
    
        var totAmt = '';
    
        if (bW != '')   totAmt += ((totAmt != '') ? ' '  : '') + bW + ' Billion';
        if (mW != '')   totAmt += ((totAmt != '') ? ' '  : '') + mW + ' Million';
        if (tW != '')   totAmt += ((totAmt != '') ? ' '  : '') + tW + ' Thousand';
        if (hW != '')   totAmt += ((totAmt != '') ? ' '  : '') + hW + ' Dollars';
    
        if (cW != '')   totAmt += ((totAmt != '') ? ' and ' : '') + cW + ' Cents';
    
    //  alert('totAmt = ' + totAmt);    // display words in a alert
    
        t1 = document.getElementById(id2).value;
    
        t2 = t1.trim();
    
        if (t2 == '')  document.getElementById(id2).value = totAmt;
    
        return false;
    }                        // function getAmtInWords( id1, id2 )
    
    // ======================== [ End Code ] ====================================
    

提交回复
热议问题