JavaScript numbers to Words

前端 未结 24 1172
死守一世寂寞
死守一世寂寞 2020-11-22 14:39

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

My Tact

24条回答
  •  隐瞒了意图╮
    2020-11-22 14:58

    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;
        }
    

提交回复
热议问题