Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a number in Indian format using Javascript

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 .

like image 503
tilak Avatar asked Apr 16 '13 12:04

tilak


People also ask

How do you format numbers in JavaScript?

JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.

What format are JavaScript numbers stored in?

Regular numbers in JavaScript are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”.


Video Answer


1 Answers

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' }); // → ₹1,23,456.79
<div id="result"></div> <div id="result1"></div>
like image 184
Tushar Gupta - curioustushar Avatar answered Sep 21 '22 15:09

Tushar Gupta - curioustushar