一个用于数字、货币和货币解析/格式化的小型JavaScript库,引用自accounting.js
-
-
formatColumn( [list] [, symbol] [, precision] [, thousand] [, decimal] [, format])
-
将数值数组格式化为货币,并格式化为统一长度(中间用空格补足)
Parameters:
Name Type Argument Default Description list
Array <可选>
[] 数值数组
symbol
string <可选>
'$' 货币符号
precision
number <可选>
2 小数位精度
thousand
string <可选>
',' 千分位符号
decimal
string <可选>
'.' 小数点符号
format
string <可选>
'%s%v' 格式
Returns:
格式化的货币字符串数组
- Type:
- Array
Example
$api.money.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], '$ ') // 输出的数组 [ '$ 123.50', '$ 3,456.49', '$ 777,888.99', '$ 12,345,678.00', '$ -5,432.00' ]
-
formatMoney( [number] [, symbol] [, precision] [, thousand] [, decimal] [, format])
-
将任何数字格式化为货币
Parameters:
Name Type Argument Default Description number
number <可选>
0 数值
symbol
string <可选>
'$' 货币符号
precision
number <可选>
2 小数位精度
thousand
string <可选>
',' 千分位符号
decimal
string <可选>
'.' 小数点符号
format
string <可选>
'%s%v' 格式
Returns:
格式化的货币字符串
- Type:
- string
Examples
$api.money.formatMoney(12345678) // $12,345,678.00
$api.money.formatMoney(4999.99, '€', 2, '.', ',') // €4.999,99 $api.money.formatMoney(4999.99, {symbol: '€', precision: 2, thousand: '.', decimal: ','}) // €4.999,99
$api.money.formatMoney(-500000, '£ ', 0) // £ -500,000
$api.money.formatMoney(5318008, { symbol: 'GBP', format: '%v %s' }) // 5,318,008.00 GBP
-
formatNumber( [number] [, precision] [, thousand] [, decimal])
-
格式化数字
Parameters:
Name Type Argument Default Description number
number <可选>
0 数值
precision
number <可选>
2 小数位精度
thousand
string <可选>
',' 千分位符号
decimal
string <可选>
'.' 小数点符号
Returns:
格式化的字符串
- Type:
- string
Example
$api.money.formatNumber(5318008) // 5,318,008 $api.money.formatNumber(9876543.21, 3, ' ') // 9 876 543.210
-
unformat(value [, decimal])
-
从任何格式化的数字/货币字符串解析为数值
Parameters:
Name Type Argument Default Description value
number | string | Array 数值,字符串,字符串数值或数值数组
decimal
string <可选>
'.' 小数点符号
Returns:
解析后的数值
- Type:
- Array.number
Example
$api.money.unformat('£ 12,345,678.90 GBP') // 12345678.9 $api.money.unformat(['£ 12,345,678.90 GBP','$ 5,678.90']) // ["12,345,679","5,679"]
-
unit( [number] [, units] [, symbol] [, precision] [, thousand] [, decimal] [, format])
-
单位转换
Parameters:
Name Type Argument Default Description number
number <可选>
0 数值
units
Array <可选>
['', '万', '亿', '万亿'] 数值数组
symbol
string <可选>
'$' 货币符号
precision
number <可选>
2 小数位精度
thousand
string <可选>
',' 千分位符号
decimal
string <可选>
'.' 小数点符号
format
string <可选>
'%s%v' 格式
Returns:
格式化的货币字符串数组
- Type:
- Array
Examples
$api.money.unit(123) // "$123.00" $api.money.unit(1234) // "$0.12万" $api.money.unit(1234525) // "$123.45万" $api.money.unit(12345253) // "$0.12亿" $api.money.unit(123452535) // "$1.23亿"
$api.money.unit(1234525,['元', '万元', '亿元']) // "$123.45万元"
$api.money.unit(1234525, '€', 2, '.', ',') // "€123,45万" $api.money.unit(1234525, {symbol: '€', precision: 2, thousand: '.', decimal: ','}) // "€123,45万"