Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any function to convert value to number format in JavaScript?

Is there function to convert a value to number format in JavaScript?

Ex: 10006.1056 to 10,006.16

like image 229
kiong Avatar asked Mar 07 '26 15:03

kiong


1 Answers

Yes there is.

Best way is to create a NumberFormat you can re-use for your locale.

For example 'en-US' (USA) or 'en-IN' (India).

To set the maximum precision digits, use the option maximumFractionDigits. If you always want to use 2 precision digits for example: 1 must be 1.00 use minimumFractionDigits too.

var number = 10006.1056;
var nf = new Intl.NumberFormat('en-US', {
    maximumFractionDigits:2, 
    minimumFractionDigits:2
});

var formattedNumber = nf.format(number); // Will output 10,006.10
like image 123
Timmetje Avatar answered Mar 09 '26 05:03

Timmetje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!