Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .toLocaleString() in Node.js

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?

like image 961
AlbertEngelB Avatar asked Apr 21 '14 14:04

AlbertEngelB


People also ask

What is the use of toLocaleString () method?

The toLocaleString() method returns a Date object as a string, using locale settings. The default language depends on the locale setup on your computer.

How do you get toLocaleString time?

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.

How do I localize a number in JavaScript?

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.


1 Answers

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.

like image 95
Aaron Dufour Avatar answered Oct 09 '22 02:10

Aaron Dufour