Posted by: Syed Abdul Baqi on: March 10, 2009
JavaScript function for converting string into Indian currency format
//function for converting string into indian currency format
function intToFormat(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
var z = 0;
var len = String(x1).length;
var num = parseInt((len/2)-1);
while (rgx.test(x1))
{
if(z > 0)
{
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
else
{
x1 = x1.replace(rgx, '$1' + ',' + '$2');
rgx = /(\d+)(\d{2})/;
}
z++;
num--;
if(num == 0)
{
break;
}
}
return x1 + x2;
}
Hope this helps someone…..
Comments are welcomed….
Thnks a lot bai
Thank you very much. This is quite useful for me too.
I made a small addition to make the function display the decimal points properly.
x2 = x.length > 1 ? ‘.’ + x[1] : ”;
// My addition for decimal point diplay
if (x2.length == 0) {
x2 = ‘.00′;
} else if (x2.length == 2) {
x2 = ‘.’ + x[1] + ’0′;
} else {
x2 = ‘.’ + x[1];
}
var rgx = /(\d+)(\d{3})/;
Yes i have used this, thanks.
after going through many posts…finally i found a GREAT POST…. really it helped me a lot… thanx…
1 | Arvind S. Kulkarni
April 22, 2010 at 6:34 am
Thank you very much. I cant say how much this has helped me. You rock, Thank you