.NET decimal.ToString (format) in Javascript

I have a string (€ #, ###. 00) that works fine with aDecimal.ToString ("€ #, ###. 00") in .NET. I wonder if anyone knows how this can be achieved with javascript

+2
source share
3 answers

There .toLocaleString() , but, unfortunately, the specification defines this as "implementation dependent" - I hate it when they do it. Thus, it behaves differently in different browsers:

 var val = 1000000; alert(val.toLocaleString()) // -> IE: "1,000,000.00" // -> Firefox: "1,000,000" // -> Chrome, Opera, Safari: "1000000" (i know, it the same as toString()!) 

Thus, you can see that you cannot rely on him because the ECMA team was too lazy to correctly define it. Internet Explorer does a better job of formatting it as a currency. You are better off with your own or someone else .


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

Tested in IE, Firefox, Safari, Chrome and Opera only in my own locale (en-GB).

+2
source

I think jQuery Globalization Plugin is approaching

+1
source

Once I had the same problem, and I decided to go with a slightly overworked (maybe stupid) way: I wrote a service that took a decimal parameter as a parameter and returned me a formatted string. The main problem was that I did not know what culture the user used in Javascript.

0
source

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


All Articles