Syed Abdul Baqi

JavaScript function for converting string into Indian currency format

Posted 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….

Advertisements

19 Responses to "JavaScript function for converting string into Indian currency format"

Thank you very much. I cant say how much this has helped me. You rock, Thank you

You are welcome. I am glad it helped you…

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})/;

Glad it helped you. And thanks for the patch.

Yes i have used this, thanks.

after going through many posts…finally i found a GREAT POST…. really it helped me a lot… thanx…

I tried it now i want to convert indian currency to number
how to convert it

@shrikant
for that just remove the commas (,) from the string.
Use your imagination. 🙂

Thanks dude..Its working fine!!!

where to put this java script code on my website to display indian currency format?

VERY HELPFULL ITS EVEN WORKING IN IE8

@JUNED
I am glad it helped you…

Thank you for your work, I am very grateful, I was using toLocaleString() but it has issues in different browsers.

@Pradeep
I am glad this snippet was useful to you…

Thanks so much. It saved me a lot of time

Thank you buddy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: