SexyMF
SexyMF

Reputation: 11155

javascript toLocaleString returns 0

I find toLocaleString function very odd.

the following line will return 0.003:

alert(0.0031.toLocaleString(2))

this line will return 0:

alert(0.00031.toLocaleString(2))

why?

Upvotes: 1

Views: 2215

Answers (7)

gurvinder372
gurvinder372

Reputation: 68393

As per spec, first two arguments to toLocaleString is locale and options

When the toLocaleString method is called with optional arguments locales and options, the following steps are taken:

When this API receive value 2 or 3, it first try to resolve locales and if not found the check within the supported locales.

The resolved locale is used as an effective locale to format the number as per the locale as per Common Locale Data Repository.

NOTE It is recommended that implementations use the locale data provided by the Common Locale Data Repository (available at http://cldr.unicode.org/).

As per documentation

The locales argument must be either a string holding a BCP 47 language tag, or an array of such language tags. If the locales argument is not provided or is undefined, the runtime's default locale is used.

A BCP 47 language tag defines a language and minimally contains a primary language code. In its most common form it can contain, in order: a language code, a script code, and a country or region code, all separated by hyphens. While the tag is not case sensitive, it is recommended to use title case for script code, upper case for country and region codes and lower case for everything else.

Finally, lookup is performed to find the best fit from the supported locales as per BCP47 logic as per locales registered in IANA language subtag registry as per various attributes of a language mentioned.

2 doesn't matches any of the supported locales so, default maximum fraction digits as per your system environment is used from available value (appears to be 3 in your case.)

Hence you get

0.00031.toLocaleString(2) //"0"

0.0031.toLocaleString(2) //"0.003"

Upvotes: 0

Rohit Choudhary
Rohit Choudhary

Reputation: 2269

number.toLocaleString() returns language sensitive representation of the number. Here in your case you are first not passing any locale information, and you are not telling this function that what is your max fraction limit.

See this example

0.0003.toLocaleString(undefined, {minimumFractionDigits: 2})
"0.00"
0.0003.toLocaleString(undefined, {minimumFractionDigits: 3})
"0.000"
0.0003.toLocaleString(undefined, {minimumFractionDigits: 4})
"0.0003

Upvotes: 0

brk
brk

Reputation: 50291

The number 2 in the argument is not making any difference. toLocaleString returns a string with a language sensitive representation of the number.

In the second case of your problem it is basically 0.000 which is equal to 0

console.log("Passing argument    " + 0.0031.toLocaleString(2))

console.log("Without argument    " + 0.0031.toLocaleString())

console.log("Zeros  " + 0.000)

Upvotes: 0

Nisal Edu
Nisal Edu

Reputation: 7591

The toLocaleString() method returns a string with a language sensitive representation of this number.

numObj.toLocaleString([locales [, options]]) has two parameters.

Reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

    var num = 0.00031;
    console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));
    console.log(num.toLocaleString(undefined, { maximumFractionDigits: 4}));

Upvotes: 0

Evgeny
Evgeny

Reputation: 6290

2 is not a valid argument for toLocaleString method of a Number. You may only pass a locale, which is a string, and and an object of options.

Options object can be used, among other things, to specify number of fraction digits:

number.toLocaleString(undefined, { maximumFractionDigits: 4 })

And the default value for it happens to be 3:

maximumFractionDigits

The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information); the default for percent formatting is the larger of minimumFractionDigits and 0.

Upvotes: 0

ShacharW
ShacharW

Reputation: 207

This is not a valid parameters to the method toLocaleString.

You can use minimumFractionDigits param like this:

var res = 0.00031.toLocaleString(undefined, {minimumFractionDigits: 4});

console.log(res);

Upvotes: 1

teddy
teddy

Reputation: 518

toLocaleString truncates and rounds the decimal number to 3 digits after the decimal.

0.0005.toLocaleString() 

for example will return 0.001. The 2 in that you pass in as a method param doesn't do anything.

Upvotes: 0

Related Questions