对单词的JavaScript数字例如,我正试图将数字转换成英语单词1234将成为:“1,234".我的策略是这样的:将数字分割为3,并将它们放到Array(finlOutPut),从右到左。转换每个组(finlOutPut数组),将三个数字组合为一个单词(这是triConvert)。如果所有三个数字都为零,则将它们转换为"dontAddBigSuffix"从右到左,添加千、百万、十亿等..如果finlOutPut单元格等于"dontAddBigSufix"(因为它只是零),不要添加单词并将单元格设置为" "(无)。它看起来很好,但是我在19000000这样的数字上遇到了一些问题9,改为:“1.9亿.不知何故,当有几个零时,它“忘记”了最后一个数字。我做错什么了?窃听器在哪里?为什么它不能完美地工作呢?<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
function update(){
var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');
var output = '';
var numString = document.getElementById('number').value;
var finlOutPut = new Array();
if (numString == '0') {
document.getElementById('container').innerHTML = 'Zero';
return;
}
if (numString == 0) {
document.getElementById('container').innerHTML = 'messeg tell to enter numbers';
return;
}
var i = numString.length;
i = i - 1;
//cut the number to grups of three digits and add them to the Arry
while (numString.length > 3) {
var triDig = new Array(3);
triDig[2] = numString.charAt(numString.length - 1);
triDig[1] = numString.charAt(numString.length - 2);
triDig[0] = numString.charAt(numString.length - 3);
var varToAdd = triDig[0] + triDig[1] + triDig[2];
finlOutPut.push(varToAdd);
i--;
numString = numString.substring(0, numString.length - 3);
}
finlOutPut.push(numString);
finlOutPut.reverse();
//conver each grup of three digits to english word
//if all digits are zero the triConvert
//function return the string "dontAddBigSufix"
for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}
添加回答
举报
0/150
提交
取消