# 货币格式化

# 1. format-money-js

安装:

npm i format-money-js

使用:

const { FormatMoney } = require('format-money-js');

// 指定默认参数
const fm = new FormatMoney({
  decimals: 2
});

// 格式化
console.log( 
  fm.from(12345.67)  // 12,345.67
);

// 指定参数
console.log( 
  fm.from(12345.67, {  // 12,345.670¥
    symbol: '¥', 
    decimals: 3, 
    append: true ,
  }) 
);

// 解析
console.log( 
  fm.un('$12,345.678')  // 12345.678
);

参数:

Name Default Type Example
grouping true Boolean 1,000 (true) vs 1000 (false)
separator , String 1,000
decimalPoint . String 1,234.56
decimals 0 Number 1,234 (0 - without decimals)
symbol None String default - 123.45, '$' - $123.45
append false String true - 1,234.56$, false - $1,234.56
leadZeros true Boolean true - 1.10, false - 1.1 (leading zeros)

# 2. 参考

本章目录