Is there any faster solution than this?
After spending some time in googling and playing with other's code, I made a quick fix and reusable function works well for numbers up to 99,99,99,999.
number2text(1234.56);
will return ONE THOUSAND TWO HUNDRED AND THIRTY-FOUR RUPEE AND FIFTY-SIX PAISE ONLY
.
function number2text(value) {
var fraction = Math.round(frac(value)*100);
var f_text = "";
if(fraction > 0) {
f_text = "AND "+convert_number(fraction)+" PAISE";
}
return convert_number(value)+" RUPEE "+f_text+" ONLY";
}
function frac(f) {
return f % 1;
}
function convert_number(number)
{
if ((number < 0) || (number > 999999999))
{
return "NUMBER OUT OF RANGE!";
}
var Gn = Math.floor(number / 10000000); /* Crore */
number -= Gn * 10000000;
var kn = Math.floor(number / 100000); /* lakhs */
number -= kn * 100000;
var Hn = Math.floor(number / 1000); /* thousand */
number -= Hn * 1000;
var Dn = Math.floor(number / 100); /* Tens (deca) */
number = number % 100; /* Ones */
var tn= Math.floor(number / 10);
var one=Math.floor(number % 10);
var res = "";
if (Gn>0)
{
res += (convert_number(Gn) + " CRORE");
}
if (kn>0)
{
res += (((res=="") ? "" : " ") +
convert_number(kn) + " LAKH");
}
if (Hn>0)
{
res += (((res=="") ? "" : " ") +
convert_number(Hn) + " THOUSAND");
}
if (Dn)
{
res += (((res=="") ? "" : " ") +
convert_number(Dn) + " HUNDRED");
}
var ones = Array("", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX","SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN","FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN");
var tens = Array("", "", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY","SEVENTY", "EIGHTY", "NINETY");
if (tn>0 || one>0)
{
if (!(res==""))
{
res += " AND ";
}
if (tn < 2)
{
res += ones[tn * 10 + one];
}
else
{
res += tens[tn];
if (one>0)
{
res += ("-" + ones[one]);
}
}
}
if (res=="")
{
res = "zero";
}
return res;
}
Is there any faster solution than this?
After spending some time in googling and playing with other's code, I made a quick fix and reusable function works well for numbers up to 99,99,99,999.
number2text(1234.56);
will return ONE THOUSAND TWO HUNDRED AND THIRTY-FOUR RUPEE AND FIFTY-SIX PAISE ONLY
.
function number2text(value) {
var fraction = Math.round(frac(value)*100);
var f_text = "";
if(fraction > 0) {
f_text = "AND "+convert_number(fraction)+" PAISE";
}
return convert_number(value)+" RUPEE "+f_text+" ONLY";
}
function frac(f) {
return f % 1;
}
function convert_number(number)
{
if ((number < 0) || (number > 999999999))
{
return "NUMBER OUT OF RANGE!";
}
var Gn = Math.floor(number / 10000000); /* Crore */
number -= Gn * 10000000;
var kn = Math.floor(number / 100000); /* lakhs */
number -= kn * 100000;
var Hn = Math.floor(number / 1000); /* thousand */
number -= Hn * 1000;
var Dn = Math.floor(number / 100); /* Tens (deca) */
number = number % 100; /* Ones */
var tn= Math.floor(number / 10);
var one=Math.floor(number % 10);
var res = "";
if (Gn>0)
{
res += (convert_number(Gn) + " CRORE");
}
if (kn>0)
{
res += (((res=="") ? "" : " ") +
convert_number(kn) + " LAKH");
}
if (Hn>0)
{
res += (((res=="") ? "" : " ") +
convert_number(Hn) + " THOUSAND");
}
if (Dn)
{
res += (((res=="") ? "" : " ") +
convert_number(Dn) + " HUNDRED");
}
var ones = Array("", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX","SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN","FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN");
var tens = Array("", "", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY","SEVENTY", "EIGHTY", "NINETY");
if (tn>0 || one>0)
{
if (!(res==""))
{
res += " AND ";
}
if (tn < 2)
{
res += ones[tn * 10 + one];
}
else
{
res += tens[tn];
if (one>0)
{
res += ("-" + ones[one]);
}
}
}
if (res=="")
{
res = "zero";
}
return res;
}
Share
Improve this question
edited Aug 24, 2016 at 9:24
mplungjan
178k28 gold badges180 silver badges240 bronze badges
asked Apr 16, 2014 at 9:57
Hardik ThakerHardik Thaker
3,0781 gold badge28 silver badges37 bronze badges
2
- 1 wat exactly u mean by "fast solution"? – user2575725 Commented Apr 16, 2014 at 10:58
- @Arvind : there are 3 functions and complex process ! fast means best optimization in terms of length of code as well execution – Hardik Thaker Commented Apr 16, 2014 at 11:07
4 Answers
Reset to default 9INDIAN PRICE CURRENCY BY WORDS BASED ON GIVEN NUMBER
125,00,00,000
One Hundred and Twenty Five Crore
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
}
console.log(price_in_words(1250000000));
console.log(price_in_words(9999999999));
The second comment has the solution. Just use:
var num =129000.9889;
var splittedNum =num.toString().split('.')
var nonDecimal=splittedNum[0]
var decimal=splittedNum[1]
var value=price_in_words(Number(nonDecimal))+" and
"+price_in_words(Number(decimal))+" paise"
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
below function support amount upto 24 digits followed by 2 decimals
function price_in_words(price) {
price = price.toString();
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 = [];
var re = /(0|([1-9]\d*))(\.\d+)?/g
if (re.test(price)) {
var arr = price.split('.');
price = arr[0];
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(handle_utlc(digit, nxtDigit, "Arab"));
break;
case 10:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 11:
words.push(handle_utlc(digit, nxtDigit, "Kharab"));
break;
case 12:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 13:
words.push(handle_utlc(digit, nxtDigit, "Nil"));
break;
case 14:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 15:
words.push(handle_utlc(digit, nxtDigit, "Padma"));
break;
case 16:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 17:
words.push(handle_utlc(digit, nxtDigit, "Shankh"));
break;
case 18:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 19:
words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] && 0 != price[digitIdx + 2] ? " and" : "") : "");
break;
case 20:
words.push(handle_utlc(digit, nxtDigit, "Thousand"));
break;
case 21:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
case 22:
words.push(handle_utlc(digit, nxtDigit, "Lakh"));
break;
case 23:
words.push(handle_tens(digit, price[digitIdx + 1]));
break;
}
}
str = words.reverse().join("")
if(arr.length > 1)
{
price = arr[1];
words= null;
for (digitIdx = price.length - 1; digitIdx >= 0; digitIdx--) {
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;
}
str += " Rupees " + words.reverse().join("") + " Paise"
}
} else str = "";
return str
}
console.log(price_in_words("1.00"));
console.log(price_in_words("12"));
console.log(price_in_words("123"));
console.log(price_in_words("1234"));
console.log(price_in_words("12345"));
console.log(price_in_words("123456"));
console.log(price_in_words("1234567"));
console.log(price_in_words("12345678"));
console.log(price_in_words("123456789"));
console.log(price_in_words("1234567890"));
console.log(price_in_words("12345678901"));
console.log(price_in_words("123456789012"));
console.log(price_in_words("1234567890123"));
console.log(price_in_words("12345678901234"));
console.log(price_in_words("123456789012345"));
console.log(price_in_words("1234567890123456"));
console.log(price_in_words("12345678901234567"));
console.log(price_in_words("123456789012345678"));
console.log(price_in_words("1234567890123456789"));
console.log(price_in_words("12345678901234567890"));
console.log(price_in_words("123456789012345678901"));
console.log(price_in_words("1234567890123456789012"));
console.log(price_in_words("12345678901234567890123"));
console.log(price_in_words("123456789012345678901234"));
console.log(price_in_words("1234567890123456789012345"));
console.log(price_in_words("12345678901234567890123456"));