本文介绍了如何将带小数点的数字输入的数量转换为javascript中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何将带有小数点的数字输入的金额转换为javascript中的单词? 我目前的代码如下: function number2text( value ){ var fraction = Math。 round(frac( value )* 100); var f_text = ; if (fraction > 0 ){ f_text = AND + Number(fraction) ; } 返回编号( value )+ + f_text; } 函数frac(f){ 返回 f% 1 ; } 函数编号(编号) { if ((number < 0 )||(数字> 999999999 )) { return 数量超出范围!; } var Gn = Math.floor(number / 10000000 ); / * Crore * / number - = Gn * 千万; var kn = Math.floor(number / 100000 ); / * lakhs * / number - = kn * 100000 ; var Hn = Math.floor(number / 1000 ); / * 千* / number - = Hn * 1000 ; var Dn = Math.floor(number / 100 ); / * 十(deca)* / number = number% 100 ; / * Ones * / var tn = Math.floor(number / 10 ); var one = Math.floor(number 10 ); var res = ; if (Gn> 0) { res + =(Number(Gn)+ CRORE); } if (kn> 0) { res + =(((res == )? : )+ Number(kn) + LAKH); } 如果(Hn> 0) { res + =(((res == )? : )+ Number(Hn) + THOUSAND); } 如果(Dn) { res + =(((res == )? : )+ Number( Dn)+ HUNDRED); } var ones = Array( , ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE , TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN, SIXTEEN, SEVENTEEN, EIGHTEEN, NINETEEN); var tens = Array( , , TWENTY, THIRTY, FOURTY, FIFTY, SIXTY, SEVENTY, EIGHTY, NINETY); if (tn> 0 || one> 0) { if (!(res == )) { res + = AND; } if (tn < 2 ) { res + = [tn * 10 + one]; } else { res + = tens [tn]; if (one> 0) { res + =( + ones [one]); } } } if (res == ) { res = ZERO; } return res; } 函数number2text( value ){ var fraction = Math.round(frac( value )* 100); var f_text = ; if (fraction > 0 ){ f_text = AND + Number(fraction) ; } 返回编号( value )+ + f_text; } 函数frac(f){ 返回 f% 1 ; } 函数编号(编号) { if ((number < 0 )||(数字> 999999999 )) { return 数量超出范围!; } var Gn = Math.floor(number / 10000000 ); / * Crore * / number - = Gn * 千万; var kn = Math.floor(number / 100000 ); / * lakhs * / number - = kn * 100000 ; var Hn = Math.floor(number / 1000 ); / * 千* / number - = Hn * 1000 ; var Dn = Math.floor(number / 100 ); / * 十(deca)* / number = number% 100 ; / * Ones * / var tn = Math.floor(number / 10 ); var one = Math.floor(number 10 ); var res = ; if (Gn> 0) { res + =(Number(Gn)+ CRORE); } if (kn> 0) { res + =(((res == )? : )+ Number(kn) + LAKH); } 如果(Hn> 0) { res + =(((res == )? : )+ Number(Hn) + THOUSAND); } 如果(Dn) { res + =(((res == )? : )+ Number( Dn)+ HUNDRED); } var ones = Array( , ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE , TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN, SIXTEEN, SEVENTEEN, EIGHTEEN, NINETEEN); var tens = Array( , , TWENTY, THIRTY, FOURTY, FIFTY, SIXTY, SEVENTY, EIGHTY, NINETY); if (tn> 0 || one> 0) { if (!(res == )) { res + = AND; } if (tn < 2 ) { res + = [tn * 10 + one]; } else { res + = tens [tn]; if (one> 0) { res + =( + ones [one]); } } } // app.alert(res); return res; } if ( this .getField ( rdoGpAmountType)。 value = = 借方) { if ( event .value!= ) { if ( event 。 value == 0) { this .getField( txtAmountInWords)。 value = ZERO ONLY; } 其他 { 此 .getField ( txtAmountInWords)。 value = number2text( this .getField ( txtDebitAmount )。 value )+ + 仅; } } else { this .getField( txtAmountInWords). value = ; } } 解决方案 看看这个家伙,他似乎走得很远。 br $> b $ b http://stackoverflow.com/questions / 5529934 / javascript-numbers-to-words [ ^ ] How to convert amount entered in digits with decimal points to words in javascript?My current code is as below:function number2text(value) { var fraction = Math.round(frac(value)*100); var f_text = ""; if(fraction > 0) { f_text = "AND "+Number(fraction); } return Number(value)+" "+f_text;}function frac(f) { return f % 1;}function Number(number){ if ((number < 0) || (number > 999999999)) { return "NUMBER OUT OF RANGE!"; } var Gn = Math.floor(number / 10000000); /* Crore */ number -= Gn * 10000000; var kn = Math.floor(number / 100000); /* lakhs */ number -= kn * 100000; var Hn = Math.floor(number / 1000); /* thousand */ number -= Hn * 1000; var Dn = Math.floor(number / 100); /* Tens (deca) */ number = number % 100; /* Ones */ var tn= Math.floor(number / 10); var one=Math.floor(number % 10); var res = ""; if (Gn>0) { res += (Number(Gn) + " CRORE"); } if (kn>0) { res += (((res=="") ? "" : " ") + Number(kn) + " LAKH"); } if (Hn>0) { res += (((res=="") ? "" : " ") + Number(Hn) + " THOUSAND"); } if (Dn) { res += (((res=="") ? "" : " ") + Number(Dn) + " HUNDRED"); } var ones = Array("", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX","SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN","FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN");var tens = Array("", "", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY","SEVENTY", "EIGHTY", "NINETY"); if (tn>0 || one>0) { if (!(res=="")) { res += " AND "; } if (tn < 2) { res += ones[tn * 10 + one]; } else { res += tens[tn]; if (one>0) { res += (" " + ones[one]); } } } if (res=="") { res = "ZERO"; } return res;}function number2text(value) { var fraction = Math.round(frac(value)*100); var f_text = ""; if(fraction > 0) { f_text = "AND "+Number(fraction); } return Number(value)+" "+f_text;}function frac(f) { return f % 1;}function Number(number){ if ((number < 0) || (number > 999999999)) { return "NUMBER OUT OF RANGE!"; } var Gn = Math.floor(number / 10000000); /* Crore */ number -= Gn * 10000000; var kn = Math.floor(number / 100000); /* lakhs */ number -= kn * 100000; var Hn = Math.floor(number / 1000); /* thousand */ number -= Hn * 1000; var Dn = Math.floor(number / 100); /* Tens (deca) */ number = number % 100; /* Ones */ var tn= Math.floor(number / 10); var one=Math.floor(number % 10); var res = ""; if (Gn>0) { res += (Number(Gn) + " CRORE"); } if (kn>0) { res += (((res=="") ? "" : " ") + Number(kn) + " LAKH"); } if (Hn>0) { res += (((res=="") ? "" : " ") + Number(Hn) + " THOUSAND"); } if (Dn) { res += (((res=="") ? "" : " ") + Number(Dn) + " HUNDRED"); } var ones = Array("", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX","SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN","FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN");var tens = Array("", "", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY","SEVENTY", "EIGHTY", "NINETY"); if (tn>0 || one>0) { if (!(res=="")) { res += " AND "; } if (tn < 2) { res += ones[tn * 10 + one]; } else { res += tens[tn]; if (one>0) { res += (" " + ones[one]); } } } //app.alert(res); return res;}if (this.getField("rdoGpAmountType").value == "Debit"){ if(event.value!=""){ if(event.value=="0") { this.getField("txtAmountInWords").value = "ZERO ONLY"; } else{this.getField("txtAmountInWords").value = number2text(this.getField ("txtDebitAmount").value)+" "+"ONLY";}}else{ this.getField("txtAmountInWords").value="";}} 解决方案 Take a look at this guy, he seems to have gotten pretty far.http://stackoverflow.com/questions/5529934/javascript-numbers-to-words[^] 这篇关于如何将带小数点的数字输入的数量转换为javascript中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 19:38