我必须将卢比数量转换为 javascript 中的单词。这里 ConvertToHundreds 是转换百,十和一的值的函数。
var aUnits = ["Thousand", "Lacs", "Crore", "Arab", "Kharab" ];
var cWords;
var nLeft = Math.floor(s);
var st = new String(nLeft);
for (var i = 0; nLeft > 0; i++) {
if (i != 0) {
cWords += ConvertToHundreds(nLeft) + " " + aUnits[i-1] + " " +cWords;
} else {
cWords = ConvertToHundreds(nLeft) + " ";
}
if (st.length <= 5) {
nLeft = Math.floor(nLeft / 1000);
} else if (st.length > 5 && st.length <= 7){
nLeft = Math.floor(nLeft / 1000) / 100;
//~ console.log(i);
i++;
} else if (st.length > 7 && st.length <= 9) {
nLeft = Math.floor(nLeft / 1000) /10000;
i+=2;
} else if (st.length > 9 && st.length <= 11){
nLeft = Math.floor(nLeft / 1000) /1000000;
i+=3;
} else {
nLeft = Math.floor(nLeft / 1000) /100000000;
i+=4;
}
}
s = Math.round(s * 100) % 100;
var rupee = "Ruppes " + cWords;
if (s > 0)
cWords = rupee + "and Paise " + ConvertToHundreds(s) + " Only";
else
cWords = rupee + "Only";
return cWords;
此代码不显示千位值。 例如:5,01,000.00只返回Rupees Five Lacs
答案 0 :(得分:0)
印度金额,价格根据输入编号显示为单词 输入:1250000000 结果:一百二十五千万 输入: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));