Convert digits into words with JavaScript

后端 未结 27 1547
我寻月下人不归
我寻月下人不归 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:58

    Lot of good answers. I needed mine for Indian (South Asian) numbering system. I modified one of the codes above - attaching it here, in case, someone else needs this. In the Indian numbering system, groups after thousands are in in 2 digits, not 3 as in the western system.

    var IS_SOUTH_ASIAN = true;
    function int_to_words(int) {
      if (int === 0) return 'zero';
    
      var ONES_WORD  = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];
      var TENS_WORD  = ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'];
      var SCALE_WORD_WESTERN = ['','thousand','million','billion','trillion','quadrillion','quintillion','sextillion','septillion','octillion','nonillion'];
      var SCALE_WORD_SOUTH_ASIAN = ['','thousand','lakh','crore','arab','kharab','neel','padma','shankh','***','***'];
    
      var GROUP_SIZE = (typeof IS_SOUTH_ASIAN != "undefined" && IS_SOUTH_ASIAN) ? 2 : 3;
      var SCALE_WORD = (typeof IS_SOUTH_ASIAN != "undefined" && IS_SOUTH_ASIAN) ? SCALE_WORD_SOUTH_ASIAN : SCALE_WORD_WESTERN;
    
    
      // Return string of first three digits, padded with zeros if needed
      function get_first_3(str) {
        return ('000' + str).substr(-(3));
      }
      function get_first(str) { //-- Return string of first GROUP_SIZE digits, padded with zeros if needed, if group size is 2, make it size 3 by prefixing with a '0'
        return (GROUP_SIZE == 2 ? '0' : '') + ('000' + str).substr(-(GROUP_SIZE));
      }
    
    
      // Return string of digits with first three digits chopped off
      function get_rest_3(str) {
        return str.substr(0, str.length - 3);
      }
      function get_rest(str) { // Return string of digits with first GROUP_SIZE digits chopped off
        return str.substr(0, str.length - GROUP_SIZE);
      }
    
      // Return string of triplet convereted to words
      function triplet_to_words(_3rd, _2nd, _1st) {
        return  (_3rd == '0' ? '' : ONES_WORD[_3rd] + ' hundred ') + 
                (_1st == '0' ? TENS_WORD[_2nd] : TENS_WORD[_2nd] && TENS_WORD[_2nd] + '-' || '') + 
                (ONES_WORD[_2nd + _1st] || ONES_WORD[_1st]);  //-- 1st one returns one-nineteen - second one returns one-nine
      }
    
      // Add to result, triplet words with scale word
      function add_to_result(result, triplet_words, scale_word) {
        return triplet_words ? triplet_words + (scale_word && ' ' + scale_word || '') + ' ' + result : result;
      }
    
      function recurse (result, scaleIdx, first, rest) {
        if (first == '000' && rest.length === 0) return result;
        var newResult = add_to_result (result, triplet_to_words (first[0], first[1], first[2]), SCALE_WORD[scaleIdx]);
        return recurse (newResult, ++scaleIdx, get_first(rest), get_rest(rest));
      }
    
      return recurse ('', 0, get_first_3(String(int)), get_rest_3(String(int)));
    }
    

提交回复
热议问题