在此INTO WORDS转换Javascript中转换美分值

时间:2013-12-26 02:14:01

标签: javascript jquery

我有一个将数值转换为单词的代码。如果用户输入的金额值为美分,我只想添加单词。

离。如果我的金额为1,000.88。单词中的值将只有一千个PESOS和EIGHTY EIGHY CENTS

更新 编辑代码以分割整个值和分值。唯一剩下的就是将美分转换为单词。

FIDDLE

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

        (function( ones, tens, sep ) {

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

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

        if ( val.length === 0 ) {
        output.textContent = 'No amount paid';
        return;  
        }

        val = val.replace(/,/g,'');
        if ( isNaN( val ) ) {
        output.textContent = 'Invalid input.';
        return;   
        }


       val = val.toString();
       var valArray = val.split('\.', 2); //splits val into two seperate integers, whole numbers and decimals, in an array.   
       val = valArray[0]; //this is the whole number
       var val2 = valArray[1]; //this should be the decimals

        if(val2 != null && val2 != ''){
        //convert the decimals here
        var str2 = 'AND TWENTY CENTS';
        }else{
        var str2 = '';
        }

        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 + ' ' + str2 + ' PESOS ONLY';       
        };

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

2 个答案:

答案 0 :(得分:2)

对不起等待,由于某种原因,这条线正在攻击我的代码。

val = parseInt( val.replace(/,/g,''), 10 );

我把它改为:

val = val.replace(/,/g,'');

它似乎具有相同的功能,同时允许我做我的事情。这是代码。我使用正则表达式在val(if语句)中测试一段时间,然后在一段时间之前和之后将val拆分成一些东西。 split函数中的2应该只允许2个字符串出来,这意味着10.20将变为'10'和'20',其中1.2.3将变为'1'和'2',并扔掉3.数组从0开始,因此valArray[0]是数组中的第一个字符串,valArray[1]是第二个字符串。

val = val.toString();
var valArray = val.split('\.', 2); //splits val into two seperate integers, whole numbers and decimals, in an array.   
val = valArray[0]; //this is the whole number
var val2 = valArray[1]; //this should be the decimals

if(val2 != null && val2 != ''){
//convert the decimals here
var str2 = 'TWENTY CENTS';
}
else{
var str2 = '';
}

更新:FIDDLE

答案 1 :(得分:0)

以下是使用美元和美分的完整版本

const num2text = {
  ones: ['', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE', 'THIRTEEN', 'FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN', 'NINETEEN'],
  tens: ['', '', 'TWENTY', 'THIRTY', 'FOURTH', 'FIFTY', 'SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY'],
  sep: ['', ' THOUSAND ', ' MILLION ', ' BILLION ', ' TRILLION ', ' QUADRILLION ', ' QUINTILLION ', ' SEXTILLION ']
};
const convert = function(val) {
  if (val.length === 0) {
    return '';
  }

  val = val.replace(/,/g, '');
  if (isNaN(val)) {
    return 'Invalid input.';
  }


  let [val1, val2] = val.split(".")
  let str2 = "";
  if (val2 != null && val2 != '') {
    //convert the decimals here
    var digits = (val2+"0").slice(0,2).split("");
    str2 = num2text.tens[+digits[0]] + " " + num2text.ones[+digits[1]]
  }
  let arr = [];
  while (val1) {
    arr.push(val1 % 1000);
    val1 = parseInt(val1 / 1000, 10);
  }
  let i = 0;
  let str = "";
  while (arr.length) {
    str = (function(a) {
      var x = Math.floor(a / 100),
        y = Math.floor(a / 10) % 10,
        z = a % 10;

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

  return str +
    ' DOLLARS ' +
    (str2 ? ' AND ' + str2 + ' CENTS' : '') +
    ' ONLY';
};


window.addEventListener("load", function() {
  document.getElementById("totalamountpaid").addEventListener("input", function() {
    document.getElementById("words").innerHTML = convert(this.value)
  });
});
<input id="totalamountpaid" placeholder="input amount" />
<br><br>
<label id="words"><label>