Is there a functionality in JavaScript to convert values into specific locale formats?
I found a way to do this at this page.
You can you toLocaleString
without using toFixed
before it. toFixed
returns a string, toLocaleString
should get a number. But you can pass an options object with toLocaleString
, the option minimumFractionDigits
could help you with the functionality toFixed
has.
50.toLocaleString('de-DE', { style: 'currency', currency: 'EUR', minimumFractionDigits: 2 });
Checkout all the other options you can pass with this function.
50.00
is a unit-less value. The best you can do is convert 50.00
to 50,00
and then append the €
yourself. Therefore, just use Number.toLocaleString()
.
var i = 50.00;alert(i.toLocaleString() + ' €'); // alerts '50.00 €' or '50,00 €'
Lots of relevant questions:
- How can I format numbers as money in JavaScript? (the big one; ~70k views)
- Convert to currency format
- Format currency using javascript
- how do i print currency format in javascript
- JavaScript: Format number/currency w/regards to culture like .NET's String.Format()? (possibly useful, if you're using ASP.NET)
- format number to price
There are locale related features described within the ECMAScript Internationalization API.
To get the float 50.0
converted to the string 50,00 €
(using the 'de-DE' locale) you need to write this:
new Intl.NumberFormat("de-DE", {style: "currency", currency: "EUR"}).format(50.0)
This API is available in all current major browsers.
For more info about the number formatting features of the Internationalization API you should read the article at MDN.