Jump to content

# Help With Currency Numerals to Words Conversion

## Recommended Posts

Hi, I need help.
(for those who don't know how does Indian Numbering System work.. the basic idea is this: 10,00,00,000... first comma comes after three zeros which is a thousand and then after two zeros.. so there is no Hundred Thousand, a hundred thousand is called One Lakh... for more details, please read the wiki article on "Indian Numbering System")
I managed to look around on internet and found myself a code which suited my need which was to convert a Number into Words with indian numbering sytem in a PDF form. It worked greatly until one day when I encountered a strange problem:
After a particular calculation which gives the result
170080.00
and when I convert it to words.. the following conversion happens:
"One Lakh Seventy Thousand and Seventy-Nine Rupees and One Hundred Paise Only"...
instead of
"One Lakh Seventy Thousand and Eighty Rupees only"
(Actual result was 170080.27, from which I substracted 0.27 to round it off to 170080.00)
===========
code is:
function number2text(value) {
var fraction = Math.round(frac(value)*100);
var f_text = "";
if(fraction == 1) {
f_text = " and "+convert_number(fraction)+" Paisa";
}
if(fraction > 1) {
f_text = " and "+convert_number(fraction)+" Paise";
}
if (Math.floor(value) == 1) {
return convert_number(value)+" Rupee"+f_text+" Only";
} else {
return convert_number(value)+" Rupees"+f_text+" Only";
}
}
function frac(f) {
return f % 1;
}
function convert_number(number)
{
if ((number < 0) || (number > 999999999))
{
return "NUMBER TOO LARGE TO CONVERT!";
}
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;
}
=========================
Thanks!

#### Share this post

##### Share on other sites

That's just a rounding error. If you want to round a number you should use something like Math.round or Math.floor.

#### Share this post

##### Share on other sites

better use Math.round instead of Math.floor.

#### Share this post

##### Share on other sites

Thanks guys! I used the Math.round(var*100)/100; to round the final number off to two decimals and it worked... Although I have to say I was really thrown off by Acrobat's ability to handle javascript code, somehow having line breaks in the code was giving me very unexpected results... (I'm using their latest one which comes with the Creative Cloud)

Until now, I was using toFixed(2) which wasn't working for me.. anyway, now things are working fine... Thanks again, good day!!!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×

×

• #### Activity

• Leaderboard
×
• Create New...