javascript - 如何在 NumberFormat 中获取小数点后的数字?

标签 javascript ecmascript-6 locale currency number-formatting

寻找解决方案以获得包括便士在内的全部货币值(value)。 numberingSystem: 'fullwide' 获取完整数字时带有字母间距。是否有任何选项可以在 NumberFormat() 中获取带有货币代码的完整数字?

预期行为:€56.123456

const CurrencyFormat = (amount = 0, currency = 'eur', digitLength = 'latn') => {
  return new Intl.NumberFormat('en-GB', {
    style: 'currency',
    currency: currency,
    numberingSystem: digitLength,
  }).format(amount)
};

console.log(CurrencyFormat(56.123456, 'eur', 'fullwide'));
console.log(CurrencyFormat(56.123456));

最佳答案

maximumFractionDigits 属性添加到包含您要允许的小数位数的选项。

const CurrencyFormat = (amount = 0, currency = 'eur', digitLength = 'latn') => {
  return new Intl.NumberFormat('en-GB', {
    style: 'currency',
    currency: currency,
    numberingSystem: digitLength,
    maximumFractionDigits: 20,
  }).format(amount)
};

console.log(CurrencyFormat(56.123456, 'eur', 'fullwide'));
console.log(CurrencyFormat(56.123456));

我通过找到单词 MaximumFractionDigits in the specification 并发现它是 can be set through the options object. 实例的一个属性找到了这一点。

关于javascript - 如何在 NumberFormat 中获取小数点后的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70689727/

相关文章:

javascript - 为什么我的链接在点击后颜色会短暂地变为红色?

javascript - 如何从多个文件导入图标以 react native 矢量图标?

ruby-on-rails - 在 Ruby/Rails 中使用依赖于语言环境的排序函数

javascript - 代码在 Firefox 中不起作用,而在 chrome 中运行良好

android - 强制使用不同的语言环境仅适用于返回堆栈中的顶级 Activity

javascript - 未捕获的 TypeError : this. getKeyCode 不是函数

javascript - 在 React Native 中使用 map 访问嵌套的 json

javascript - 单击时重新评估包含的部分

javascript - 如何在不将其声明为全局的情况下使用多个类来修改单个数据源?

javascript - 如何将对象数组转换为对象列表