Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number().toLocaleString() has different format in different browsers

I format a float to a locale string (Euro) and there are very different results in every browser. Is it possible to fix without an own function?

var sum=2282.0000;
var formated_sum = Number(sum.toFixed(2)).toLocaleString("de-DE", {style: "currency", currency: "EUR"});

Firefox result: 2.282,00 €

Chrome result: 2.282 €

IE result: 2.282,00 €

Safari result: 2282 €

Safari results are very much wrong, chrome results are not so much bad. Any Idea how to fix that without writing an own function for formatting?


This question may already have an answer here: Inconsistent behavior of toLocaleString() in different browser No, my question is different because i am searching for a solution for Currency, not DATE

like image 224
Viktor Avatar asked Apr 29 '15 11:04

Viktor


People also ask

What does the toLocaleString () method do in JS?

The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .

How does toLocaleString determine locale?

The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running.

Does toLocaleString round?

The toLocaleString() method will round the resulting value if necessary. The toLocaleString() method does not change the value of the original number.

What is the tolocalestring method in JavaScript?

The toLocaleString method is a convenient functionality for language-sensitive formatting of dates, numbers, times, currencies, and data structures like arrays and typed arrays in JavaScript. The toLocaleString method uses the environment’s default locale for formatting. However, you can use it to format in a language different from the default.

Is tolocalestring supported by ES5?

For those that don't, the locales and options arguments must both be ignored. You can check support by testing if illegal language tags are rejected with a RangeError: However, prior to ES5.1, implementations were not required to throw a range error exception if toLocaleString is called with illegal arguments.

How to format large numbers of numbers in JavaScript?

A string with a language-sensitive representation of the given number. In implementations with Intl.NumberFormat, this is equivalent to new Intl.NumberFormat (locales, options).format (number). When formatting large numbers of numbers, it is better to create a Intl.NumberFormat object and use the function provided by its format property.


1 Answers

ECMA 262 specifies that the function is implementation dependent and takes no arguments.

Produces a String value that represents this Number value formatted according to the conventions of the host environment’s current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.

NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.

It is also in ECMA internationalization API specification (which for Number.prototype.toLocaleString supersedes ECMA 262 but accepts 2 arguments)

This definition supersedes the definition provided in ES5, 15.7.4.3.

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

Let x be this Number value (as defined in ES5, 15.7.4). If locales is not provided, then let locales be undefined. If options is not provided, then let options be undefined. Let numberFormat be the result of creating a new object as if by the expression new Intl.NumberFormat(locales, options) where Intl.NumberFormat is the standard built-in constructor defined in 11.1.3. Return the result of calling the FormatNumber abstract operation (defined in 11.3.2) with arguments numberFormat and x. The value of the length property of the toLocaleString method is 0.

Besides, mdn specifies that Safari has no support for it.

As for a viable solution see this answer on SO

like image 156
axelduch Avatar answered Nov 15 '22 00:11

axelduch