JavaScript numbers to Words

前端 未结 24 1509
死守一世寂寞
死守一世寂寞 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 15:05

    I think this will help you

        aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy","Eighty", "Ninety"];
    
        aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" ];
        var aUnits = "Thousand";
        function convertnumbertostring(){
           var num=prompt('','enter the number');
           var j=6;
           if(num.length 99) {
              /* Hundreds. */
              cNum = String(num);
              nNum = Number(cNum.charAt(0));
              cWords += aOnes[nNum] + " Hundred";
              num %= 100;
              if (num > 0){
                 cWords += " and "
              }
           }
    
           if (num > 19) {
              /* Tens. */
              cNum = String(num);
              nNum = Number(cNum.charAt(0));
              cWords += aTens[nNum - 2];
              num %= 10;
              if (num > 0){
                 cWords += "-";
              }
           }
           if (num > 0) {
              /* Ones and teens. */
              nNum = Math.floor(num);
              cWords += aOnes[nNum];
           }
           return(cWords);
    
        }
        function ConvertToWords(num)
        {
           var cWords;
           for (var i = 0; num > 0; i++) { 
               if (num % 1000 > 0) {
                  if (i != 0){
                     cWords = ConvertToHundreds(num) + " " + aUnits + " " + cWords;
                  }else{
                     cWords = ConvertToHundreds(num) + " ";
                  }        
               }
               num = (num / 1000);
           }
             return(cWords);
        }
    

提交回复
热议问题