So I was writing a small helper method to convert numbers into a valid money format ($xx,xxx.xx
) using .toLocaleString()
. Everything works as expected when using it inside Chrome, however it seems completely broken when using inside Node.js.
Example:
var n = 6000 console.log( n.toLocaleString('USD', { style: 'currency', currency: "USD", minimumFractionDigits : 2, maximumFractionDigits : 2 }) );
If you run this in the browser, it prints $6,000.00
. If you run this snippet inside of Node.js REPL or application, it returns 6000
as a String.
Guessing this is a bug with Node.js? Is there a work around you could do here?
The toLocaleString() method returns a Date object as a string, using locale settings. The default language depends on the locale setup on your computer.
To get the current date and time in JavaScript, you can use the toLocaleString() method, which returns a string representing the given date according to language-specific conventions. To display only the time, you can use the toLocaleTimeString() method.
In JavaScript, toLocaleString() is a Number method that is used to convert a number into a locale-specific numeric representation of the number (rounding the result where necessary) and return its value as a string.
Based on this issue it appears that it was decided that shipping node.js with internationalization would make it too large. You can npm install intl
and require that, and it will replace toLocaleString
with a version that works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With