Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range error with toLocaleString() with maximumNumber of digits 0

Tags:

javascript

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

like image 230
toms Avatar asked Dec 08 '16 17:12

toms


1 Answers

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 });
like image 179
toms Avatar answered Nov 12 '22 09:11

toms