Fuzzyma
Fuzzyma

Reputation: 8484

Number.toLocaleString(), minimumFractionDigits acts like maximum

I have this code:

10.00000001.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 2})

I expect it to return '10.00000001' but what I get is 10.00. When I change the minimum the accurracy changes accordingly.

The minimum acts like its a maximum.

Setting maximumFractionDigitsdoes not change anything. It is ignored completely.

I tested this with node 8.1.4 and in FF Quantum.

Any ideas why toLocaleString acts so strange?

Upvotes: 0

Views: 1410

Answers (1)

nerding_it
nerding_it

Reputation: 704

According to documentation https://www.jsman.net/manual/Standard-Global-Objects/Number/toLocaleString, minimum is the number of digits you want in decimal. Below two examples will give clear understanding

var n = 10.00000001;
var x;
// It will give 8 decimal point because min is 0 (i.e. Atleast it should have one decimal point) and max it can have till 8 
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 0, maximumFractionDigits: n.toString().split('.')[1].length});
console.log(x);
// If you put value 2.301 it gives 2.3 since it omits 0 in 2.30 (i.e. 
n = 2.311;
// It will give 1 decimal point because min to max is 1
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 1, maximumFractionDigits: 2});
console.log(x);
// It will give 1 decimal point eventhough we didn't have decimal points
n = 2;
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 1});
console.log(x);

Upvotes: 1

Related Questions