在Node.js中使用.toLocaleString() [英] Using .toLocaleString() in Node.js

查看:72
本文介绍了在Node.js中使用.toLocaleString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一个小的辅助方法,使用 .toLocaleString()将数字转换为有效的货币格式( $ xx,xxx.xx ).在Chrome浏览器中使用时一切正常,但是在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.

示例:

var n = 6000
console.log( n.toLocaleString('USD', {
  style: 'currency',
  currency: "USD",
  minimumFractionDigits : 2,
  maximumFractionDigits : 2
}) );

如果在浏览器中运行此命令,它将显示 $ 6,000.00 .如果您在Node.js REPL或应用程序中运行此代码段,它将以字符串形式返回 6000 .

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.

猜到这是Node.js的错误吗?您可以在这里进行周围的工作吗?

Guessing this is a bug with Node.js? Is there a work around you could do here?

推荐答案

基于此问题看来,决定将具有国际化的node.js交付会使其过大.您可以 npm install intl 并要求这样做,它将用有效的版本替换 toLocaleString .

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.

这篇关于在Node.js中使用.toLocaleString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆