1、兼容低于iOS16.4版本
val = "123456.123456";
let num = val; // 除小数部分
let decimal = ""; // 小数部分
if (val.includes(".")) {
num = val.split(".")[0];
decimal = `.${val.split(".")[1]}`;
}
num = num.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
val = `${num}${decimal}`;
结果:'123,456.123456'
2、纯正则,不兼容低于iOS16.4版本
来源:https://blog.csdn.net/qq_19309473/article/details/129877569
// iOS16.4+支持
"123456.123456".replace(/(?<!.*\..*)(\d)(?=(\d{3})+($|\.))/g, '$1,')
结果:'123,456.123456'
↓↓↓ 各位大佬点点赞
0 个评论
要回复文章请先登录或注册