简体   繁体   中英

Why Number#toLocaleString() doesn't work as expected in both webkit console and node console?

var a = 1234567;

console.log(a.toLocaleString())

#=> '1234567'

Why there's no difference between toLocaleString() and toString in this test?

I was just searching for an answer to this myself. The short answer is that Node.js builds don't have the library for internalization needed for Number.toLocaleString included by default because it substantially increases the binary size.

You can build Node from source with the library included if you really need it. This other answer has more details if you are interested.

toLocaleString is just a culture-aware version of toString. If you see no difference between them, then the object you call them on has the same representation both in your local culture and in the invariant culture. This pair of methods will give different results in countries where comma is used in floating point numbers instead of dot.

1.23.toLocaleString(); // "1,23"
1.23.toString(); // "1.23"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM