在此INTO WORDS转换Javascript中转换美分值 [英] Convert cents value in this INTO WORDS conversion Javascript

查看:48
本文介绍了在此INTO WORDS转换Javascript中转换美分值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将数值转换成单词的代码.我只想在用户输入的金额中包含美分的情况下,在文字中添加美分.

I have a code which convert numerical value into words. I just want to add cents in words if user inputs an amount which have a cents in value.

例如如果我有1,000.88.文字值将为仅一千比索和八十埃分.

ex. If I have an amount of 1,000.88. The value in words will be ONE THOUSAND PESOS AND EIGHTY EIGHY CENTS ONLY.

更新: 对代码进行了编辑,以分割全部价值和美分价值.剩下的唯一一件事就是将美分转换为单词.

UPDATE: Code was edited to split the whole value and cents value. The only thing remaining is the conversion of cents into words.

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

推荐答案

抱歉,由于某种原因,该行正在攻击我的代码.

Sorry for the wait, for some reason this line was attacking my code.

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]是第二个字符串.

which seems to have the same functionality while allowing me to do my thing. Here is the code. I use regexes to test for a period in val (the if statement), and then to split val into things before and after a period. The 2 in the split function should only allow for 2 strings to come out of it, meaning 10.20 would become '10' and '20' where 1.2.3 would become '1' and '2', and throw away 3. Arrays start at 0, so valArray[0] is the first string in the array, and valArray[1] is the second one.

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

这篇关于在此INTO WORDS转换Javascript中转换美分值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆