How to format numbers using JavaScript?

I want to format the number using javascript as below:

10.00=10,00 1,000.00=1.000,00 
+6
source share
3 answers

Each browser supports Number.prototype.toLocaleString() , a method designed to return a localized string from a number. However, the specification defines it as follows:

Produces a string value that represents the value of a number formatted according to the conventions of the current locale of the host environment. This function is implementation dependent and valid, but is not encouraged to return the same as toString .

Dependence on implementation means that the supplier must understand what the result will look like and will lead to compatibility issues.

Internet Explorer (from IE 5.5 to IE 9) is closest to what you want and formats a currency-style number - the thousands separator and is fixed at 2 decimal places.

Firefox (2+) formats thousands and decimal places, but only if applicable.

Opera, Chrome, and Safari are displayed in the same way as toString() - without thousands separators, decimal if required.

Decision

I came up with the following code (based on my old answer ) to try to normalize the results to work as an Internet Explorer method:

 (function (old) { var dec = 0.12 .toLocaleString().charAt(1), tho = dec === "." ? "," : "."; if (1000 .toLocaleString() !== "1,000.00") { Number.prototype.toLocaleString = function () { var neg = this < 0, f = this.toFixed(2).slice(+neg); return (neg ? "-" : "") + f.slice(0,-3).replace(/(?=(?!^)(?:\d{3})+(?!\d))/g, tho) + dec + f.slice(-2); } } })(Number.prototype.toLocaleString); 

This will use the built-in localization of the browser, if available, while gracefully humiliating itself to the default browser language in other cases.

Working demo: http://jsfiddle.net/R4DKn/49/

+18
source

I know this solution using NumberFormat, but I need to convert the values ​​to a string. https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/NumberFormat

 // Remove commas number = "10,000.00".replace(/,/g, ''); // Create a NumberFormat type object var formatter = new Intl.NumberFormat('de-DE', { minimumFractionDigits: 2 }); // Apply format console.log(formatter.format(number)); output:10.000,00 
+3
source

Javascript does not provide this functionality itself, but there are a number of third-party functions around which you can do what you want.

Note that no matter what method you use, you should be careful to use only the resulting string for display purposes - this will not be a valid number in Javascript after converting the decimal point to comma.

The quickest solution I can offer is to use the number_format() function, written by the phpJS people. They have implemented versions of Javascript to load commonly used PHP functions, including number_format() , and this function will do exactly what you want.

See the link here: http://phpjs.org/functions/number_format

I would not worry about taking the entire phpJS library (in any case, this is a lot of dubious value), but just take the function with 20 second lines shown on the page linked above and paste it into your application.

If you want a more flexible solution, there are many JS functions around which the printf() function from C. is imitated. There is already a good question regarding SO. See here: JavaScript equivalent of printf / string.format

Hope this helps.

+1
source

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


All Articles