kiong
kiong

Reputation: 116

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

Upvotes: 3

Views: 94

Answers (1)

Timmetje
Timmetje

Reputation: 7694

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

Upvotes: 2

Related Questions