我是一名业余程序员。我刚刚从VBS切换到JS,我是 试图将数字转换为文本,我陷入困境,没有 想法该怎么做。
<html>
<body>
<script language="javascript">
var x = prompt("Please enter a number");
var str=x.toString();
var i;
for i <5, i++;{
var c=parseInt(str,i,1);
}
function main(mainvalue,addstring) {
if mainvalue=1 {
var a=(" one");
}elseif mainvalue=2 {
var a=(" two");
}elseif mainvalue=3 {
var a=(" three") ;
}elseif mainvalue=4 {
var a=(" four");
}elseif mainvalue=5 {
var a=(" five");
}elseif mainvalue=6 {
var a=(" six");
}elseif mainvalue=7 {
var a=(" seven") ;
}elseif mainvalue=8 {
var a=(" eight");
}elseif mainvalue=9 {
var a=(" nine") ;
}
main=(a + addstring);
}
var thousands=parseInt(c/1000);
var strvalue = main( thousands, " thousand");
document.write(main(thousands," thousand" ));
var c=c-(thousands*1000) ;
var hundreds=parseInt(c/100);
document.write(" and ");
var strvalue1 = main( hundreds, " hundred") ;
document.write(main(hundreds," hundred"));
var c=c-(hundreds*100);
var tens=parseInt(c/10);
var strvalue2 = main( tens, "ty");
document.write(main(tens,"ty
var c=c-(tens*10);
var ones=parseInt(c);
var strvalue3 = main(ones,"");
document.write(main(ones,""));
</script>
</body>
</html>
答案 0 :(得分:0)
这样的事情会做到;你可以通过插入&#34;和&#34;来进一步发展。在适当的时候。
var units = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
var places = ["thousand ", "hundred ", "", ""];
var input = prompt("Please enter a number from 0 to 9999");
var number = input.match(/^\d{1,4}$/);
var result = "";
if (number) {
var digits = number[0].match(/\d/g);
for (var place = 4 - digits.length; place < 4; place++) {
var digit = parseInt(digits.shift());
if (digit) {
if (place != 2) result += units[digit] + " " + places[place];
else {
if (digit != 1) result += tens[digit] + " ";
else digits[0] = "1" + digits[0];
}
}
}
if (result == "") result = "zero";
}
else result = "invalid input";
alert(result);
&#13;