印度金额,价格货币根据输入数字显示为单词输入:1250000000 结果:12.5 亿 输入:1250000 结果:12.50 Lac(s)
function price_in_words(price) {
var sglDigit = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
dblDigit = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
tensPlace = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"],
handle_tens = function(dgt, prevDgt) {
return 0 == dgt ? "" : " " + (1 == dgt ? dblDigit[prevDgt] : tensPlace[dgt])
},
handle_utlc = function(dgt, nxtDgt, denom) {
return (0 != dgt && 1 != nxtDgt ? " " + sglDigit[dgt] : "") + (0 != nxtDgt || dgt > 0 ? " " + denom : "")
};
var str = "",
digitIdx = 0,
digit = 0,
nxtDigit = 0,
words = [];
if (price += "", isNaN(parseInt(price))) str = "";
else if (parseInt(price) > 0 && price.length <= 10) {
for (digitIdx = price.length - 1; digitIdx >= 0; digitIdx--) switch (digit = price[digitIdx] - 0, nxtDigit = digitIdx > 0 ? price[digitIdx - 1] - 0 : 0, price.length - digitIdx - 1) {
case 0:
words.push(handle_utlc(digit, nxtDigit, ""));
break;
case 1:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 2:
words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] && 0 != price[digitIdx + 2] ? " and" : "") : "");
break;
case 3:
words.push(handle_utlc(digit, nxtDigit, "Thousand"));
break;
case 4:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 5:
words.push(handle_utlc(digit, nxtDigit, "Lakh"));
break;
case 6:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 7:
words.push(handle_utlc(digit, nxtDigit, "Crore"));
break;
case 8:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 9:
words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] || 0 != price[digitIdx + 2] ? " and" : " Crore") : "")
}
str = words.reverse().join("")
} else str = "";
return str
}
alert(price_in_words(1250000000));
function price_in_num_words(price) {
var result = "",
priceInNum = parseFloat(price),
priceStr = new String(price);
if (!isNaN(priceInNum)) {
var x = priceStr.split("."),
intLength = x[0].length;
result = intLength <= 3 ? priceInNum.toFixed(1) : intLength <= 5 ? (priceInNum / 1e3).toFixed(1) + " Thousand" : intLength <= 7 ? (priceInNum / 1e5).toFixed(2) + " Lac(s)" : intLength <= 9 ? (priceInNum / 1e7).toFixed(2) + " Cr(s)" : "More than One Hundred Cr(s)"
}
return result
}
alert(price_in_num_words(1250000));