首页 > 编程知识 正文

js 字符串转换,js转字符

时间:2023-05-03 17:57:50 阅读:274435 作者:3952

工作需要在网上找了个千分符转换,现在忘了是那个大哥的杰作了,直接贴上吧,最后加了个如果没有值默认返回零
formatData(num, decimals, thousandsSep) {
if (isNaN(num)) {
num = ‘0.00’
}
num = num + ‘’
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals) // 保留的位数一定是有限位数的正整数
const sep = (typeof thousandsSep === ‘undefined’) ? ‘,’ : thousandsSep
const s = num.toString().replace(/,/g, ‘’) // 字符串,将,变成’’;
const p = parseFloat(s) // 解析一个字符串,并返回一个浮点数
const n = isNaN§ ? 1 : p
const formatNum = n.toFixed(prec).toString().replace(/(d)(?=(d{3})+.)/g, function($0, $1) {
return $1 + sep
})
return num ? formatNum : ‘0’
},

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。