数字到文字 将数字转换为显示的 JavaScript 中的单词 使用 HTML 页面从用户处获取输出 轻松简单地使用 for 和 switch 语句
这可能对您有帮助,我认为没有比这更直接的方法了。
var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");
function number2words(n){
if (n < 20) return num[n];
var digit = n%10;
if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " and " + number2words(n%100));
return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
}
https://www.codegrepper.com/code-examples/javascript/convert+number+to+words+in+javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<br><br>
<h1>NUMBER TO WORDS</h1>
<input type="5ber" id="nm1" placeholder="number"><br><br>
<input type="button" value="To Words" id="optin" onclick="pldstrng()">
<p id="value" style="color: blueviolet;"></p>
<script>
function pldstrng() {
var k = document.getElementById("nm1").value
var a = String(k)
for (let i = 0; i < a.length; i++) {
switch (a[i]) {
case "1":
document.getElementById("value").innerHTML+="one"+" "
break
case "2":
document.getElementById("value").innerHTML+="two"+" "
break
case "3":
document.getElementById("value").innerHTML+="three"+" "
break
case "4":
document.getElementById("value").innerHTML+="four"+" "
break
case "5":
document.getElementById("value").innerHTML+="five"+" "
break
case "6":
document.getElementById("value").innerHTML+="six"+" "
break
case "7":
document.getElementById("value").innerHTML+="siven"+" "
break
case "8":
document.getElementById("value").innerHTML+="eight"+" "
break
case "9":
document.getElementById("value").innerHTML+="nine"+" "
break
case "0":
document.getElementById("value").innerHTML+="zero"+" "
break
default :
document.getElementById("value").innerHTML="enter a number / digit"+" "
}
}
}
</script>
</body>
</html>
基于@Grim Ranger 的答案添加:
使其变得更加复杂,但如果您也需要它,希望它可以让您不必重写它。
function number_to_words(n: number): string {
const num = "zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen".split(",");
const tens = "twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety".split(",");
if (n < 20) return num[n];
const digit = n % 10;
if (n < 100) return tens[Math.floor((n / 10)) - 2] + (digit ? "-" + num[digit] : "");
if (n < 1000) return num[Math.floor((n / 100))] + " hundred" + (n % 100 == 0 ? "" : " and " + number_to_words(n % 100));
if (n < 1000000) return number_to_words(Math.floor((n / 1000))) + " thousand" + (n % 1000 == 0 ? "" : ((n % 1000 < 100 ? " and " : " ") + number_to_words(n % 1000)));
return number_to_words(Math.floor((n / 1000000))) + " million" + (n % 1000000 == 0 ? "" : ((n % 1000000 < 100 ? " and " : " ") + number_to_words(n % 1000000)));
}