Displaying a number in Indian format using Javascript

tilak picture tilak · Apr 16, 2013 · Viewed 64.4k times · Source

I have the following code to display in Indian numbering system.

 var x=125465778;
 var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

Am getting this output :125,465,778.

I need output like this: 12,54,65,778.

Please help me to sort out this problem .

Answer

i'm late but i guess this will help :)

you can use Number.prototype.toLocaleString()

Syntax

numObj.toLocaleString([locales [, options]])

var number = 123456.789;
// India uses thousands/lakh/crore separators
document.getElementById('result').innerHTML = number.toLocaleString('en-IN');
// → 1,23,456.789

document.getElementById('result1').innerHTML = number.toLocaleString('en-IN', {
    maximumFractionDigits: 2,
    style: 'currency',
    currency: 'INR'
});
// → Rs.123,456.79
<div id="result"></div>
<div id="result1"></div>