-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
将数字格式化成金额的形式并四舍五入保留 x 位小数 #10
Labels
Comments
补充一种快捷格式化方法: const num = 123456789
// 格式化千分位输出
num.toLocaleString() // "123,456,789"
// 格式化为千分位带$符号输出
num.toLocaleString("en-US",{style:"currency",currency:"USD"}) // "$123,456,789.00"
// 格式化为带¥符号输出
num.toLocaleString("zh-Hans-CN",{style:"currency",currency:"CNY"}) // "¥123,456,789.00" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
将数字格式化成金额的形式并四舍五入保留 x 位小数
一. 保留 x 位小数
如:
1234.781
转化为1,234.78
tofix 存在的问题: JavaScript 浮点数陷阱及解法
另外的办法:Math.round(X * 保留的位数) / 保留的位数
二. 将数字格式化成金额的形式
三. 分割小数整数部分最终合并
另外还需要处理一些问题:没有小数,或者小数尾数不足的时候补 0
完整代码
The text was updated successfully, but these errors were encountered: