Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1.28 KB

number-to-currency.md

File metadata and controls

40 lines (27 loc) · 1.28 KB
标题 标签
NumberToCurrency(数字转金额格式) number(数字)

数字转金额格式

  • 使用Number.prototype.toLocaleString()实现数字转金额。
  • 第一个参数设置cmn-Hans-CN或者zh-CN使地区使用中国,第二个参数中的style属性设置为currency实现金额格式,并设置currency: 'CNY'使用人民币格式 更多格式可查看

代码如下

const numberToCurrency = (num, format, options) =>
  num.toLocaleString(format, options);

ts代码如下:

调用方式

numberToCurrency(10000000, 'cmn-Hans-CN', {
  style: 'currency',
  currency: 'CNY'
});
// "¥10,000,000.00"

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/number-to-currency.html"></iframe>