Number (). ToLocaleString () has a different format in different browsers

I format the float to a locale string (Euro), and the results in each browser are very different. Can I fix it without my 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 €

Result IE: 2.282,00 €

Safari result: 2282 €

Safari's results are very wrong, chrome results are not so bad. Any idea how to fix this without writing your own formatting function?


This question may already have the answer here: Inconsistent behavior of toLocaleString () in different browsers No, my question is different from what I am looking for a solution for Currency, not DATE

+5
source share
3 answers

ECMA 262 indicates that the function is implementation dependent and accepts no arguments.

Produces a string value that represents this numeric value, formatted according to the conventions of the current host localization environment. This function is implementation dependent, and it is acceptable, but it is not recommended that it return the same as toString.

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

Also in the ECMA internationalization API specification (which for Number.prototype.toLocaleString replaces ECMA 262 but takes 2 arguments)

This definition replaces the definition given in ES5, 15.7.4.3.

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

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

In addition, mdn indicates that Safari does not support it.

For a viable solution, see this answer to SO

+1
source

I worked on this using Number.toLocaleString-with-Locales shim

It is simple, very light and sufficient for my needs.

0
source

Accounting.js, as Reeno mentioned, is the solution! Thanks Reeno!

-3
source

Source: https://habr.com/ru/post/1013665/


All Articles