toms
toms

Reputation: 2062

Range error with toLocaleString() with maximumNumber of digits 0

The following works in Chrome:

var formatted = (value * 1).toLocaleString('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 });

but IE/Edge and Firefox throw the error:

RangeError: invalid digits value: 0

The Mozilla docs for toLocaleString:

maximumFractionDigits: The maximum number of fraction digits to use. Possible values are from 0 to 20

Upvotes: 32

Views: 17546

Answers (1)

toms
toms

Reputation: 2062

A closer read of the docs for NumberFormat shows that, for USD currency, the default value for minimumFractionDigits is 2. Setting both minimumFractionDigits and maximumFractionDigits to 0 fixed the issue. From this is seems that in IE and Edge maximumFractionDigits must be >= minimumFractionDigits, when specifying currency (at least)

var formatted = (value * 1).toLocaleString('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, minimumFractionDigits: 0 });

Upvotes: 78

Related Questions