Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...