当金额存储以分单位需要转换为元时,需要除以100。
JS 中如果直接除 100,显示的格式比较乱
> 1001 / 100
10.01
> 1000 / 100
10
统一保留两位小数
function formatMoney(money) {
return (money * 1.0 / 100).toFixed(2)
}
> formatMoney(1000)
"10.00"
> formatMoney(1001)
"10.01"
当金额存储以分单位需要转换为元时,需要除以100。
JS 中如果直接除 100,显示的格式比较乱
> 1001 / 100
10.01
> 1000 / 100
10
function formatMoney(money) {
return (money * 1.0 / 100).toFixed(2)
}
> formatMoney(1000)
"10.00"
> formatMoney(1001)
"10.01"