javascript - 如何在 Number to Word 转换中添加美分
问题描述
我已经有一个可以将数字转换为单词的代码,但是当我输入小数点时,它被计为“0”,我还需要在转换中添加美分。
function numberToEnglish( n ) {
var string = n.toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';
/* Remove spaces and commas */
string = string.replace(/[,|.]/g,"");
/* Check number if Zero? */
if( parseInt( string ) === 0 ) {
return 'Zero';
}
/* Array of units as words */
units = [ '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' ];
/* Array of tens as words */
tens = [ '', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety' ];
/* Array of scales as words */
scales = [ '', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion', 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quatttuor-decillion', 'Quindecillion', 'Sexdecillion', 'Septen-decillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion', 'Centillion' ];
/* Split user arguemnt into 3 digit chunks from right to left */
start = string.length;
chunks = [];
while( start > 0 ) {
end = start;
chunks.push( string.slice( ( start = Math.max( 0, start - 3 ) ), end ) );
}
/* Check if function has enough scale words to be able to stringify the user argument */
chunksLen = chunks.length;
if( chunksLen > scales.length ) {
return '';
}
/* Stringify each integer in each chunk */
words = [];
for( i = 0; i < chunksLen; i++ ) {
chunk = parseInt( chunks[i] );
if( chunk ) {
/* Split chunk into array of individual integers */
ints = chunks[i].split( '' ).reverse().map( parseFloat );
/* If tens integer is 1, i.e. 10, then add 10 to units integer */
if( ints[1] === 1 ) {
ints[0] += 10;
}
/* Add scale word if chunk is not zero and array item exists */
if( ( word = scales[i] ) ) {
words.push( word );
}
/* Add unit word if array item exists */
if( ( word = units[ ints[0] ] ) ) {
words.push( word );
}
/* Add tens word if array item exists */
if( ( word = tens[ ints[1] ] ) ) {
words.push( word );
}
/* Add 'and' string after units or tens integer if: */
if( ints[0] || ints[1] ) {
/* Chunk has a hundreds integer or chunk is the first of multiple chunks */
if( ints[2] || (i + 1) > chunksLen ) {
words.push( and );
}
}
/* Add hundreds word if array item exists */
if( ( word = units[ ints[2] ] ) ) {
words.push( word + ' Hundred' );
}
}
}
return words.reverse().join( ' ' );
}
当我输入 123,234.20 时,我期望输出为 123,234 20 美分,但实际输出为 12323230420
解决方案
您将点和逗号视为同一事物(都用“”替换)尝试将它们分开,以便您的代码也检查点。
确切地说,这条线
string = string.replace(/[,|.]/g,"");
你的号码变成了 12323420 而不是 123234.20
推荐阅读
- mysql - 如何复制 SQL 数据库表中的所有行并更改其中一列中的数据?
- ios - 无法使用 CoreData 将数据从 UITableView 传递到 ViewController
- angular - Angular 5 应用程序有时会出现此错误 :: Uncaught ReferenceError: vendor_c188a8d396e6b8d23040 is not defined
- git - git reset --soft HEAD^ 用于初始提交
- python - 使用定义变量创建表
- android - 如何使用 Splash Activity 的持续时间旋转位图?
- java - 如何按日期和时间对列表进行排序?
- ms-access - DLOOKUP 函数多条件
- javascript - 无法调用在单独文件中定义的函数
- python-3.4 - Python 使用 RC522 写入个人用户 ID