我正在寻找一种从文本框中获取数字输入并将其转换为文本行的方法。例如 -
输入金额£[950000]
NINE HUNDRED AND FIFTY THOUSAND
我是如何实现这一目标的?
答案 0 :(得分:6)
看看this library。这应该做你需要的。
答案 1 :(得分:0)
使用此代码它为您提供了背后的想法---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>numericToEnglish</title>
</head>
<body>
<script type="text/javascript">
aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen" ];
function ConvertToHundreds(num)
{
var cNum, nNum;
var cWords = "";
num %= 1000;
if (num > 99) {
/* Hundreds. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aOnes[nNum] + " Hundred";
num %= 100;
if (num > 0)
cWords += " and "
}
if (num > 19) {
/* Tens. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aTens[nNum - 2];
num %= 10;
if (num > 0)
cWords += "-";
}
if (num > 0) {
/* Ones and teens. */
nNum = Math.floor(num);
cWords += aOnes[nNum];
}
return cWords;
}
function ConvertToWords(num)
{
if(num == "")
{
alert("enter num")
return
}
var aUnits = [ "Thousand", "Million", "Billion", "Trillion", "Quadrillion" ];
var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
var nLeft = Math.floor(num);
for (var i = 0; nLeft > 0; i++) {
if (nLeft % 1000 > 0) {
if (i != 0)
cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
else
cWords = ConvertToHundreds(nLeft) + " " + cWords;
}
nLeft = Math.floor(nLeft / 1000);
}
num = Math.round(num * 100) % 100;
if (num > 0)
cWords += ConvertToHundreds(num) + " Cents";
else
cWords += "Zero Cents";
document.getElementById("divVal").innerHTML = cWords
return cWords;
}
</script>
<input type=text id=Number name=Number>
<input type=button id=btnOk name=btnOk value="Number To English" onclick='ConvertToWords(document.getElementById("Number").value)' />
<div id="divVal"></div>
</body>
</html>