js操作将数字转换成 , 逗号分割的字符串并追加‘万’字
// 将数字使用 , 分割 例:121356 -> 121,356
export function breakNumComma(nums, ws) {
// 如果是纯数字 或 字符串型纯数字 isNaN() 返回 false,包含任意任意非数字字符(包含一个小数点除外)均返回 true
// 对于 null 空字符 以及 空格, Number() 返回 0, isNaN() 返回 false
if (nums && !isNaN(nums) && Number(nums) != 0) {
var w = "";
var ns = "";
// console.log(`nums -> ${nums}`)
nums = Number(nums).toFixed(2);
if (nums > 99999999 || nums < -99999999) {
nums = (nums / 10000).toFixed(2);
if (!ws) w = "<span style='font-size: 12px;'> 万</span>";
else w = ' 万'
if (nums > 99999999 || nums < -99999999) {
nums = (nums / 10000).toFixed(2);
if (!ws) w = "<span style='font-size: 12px;'> 亿</span>";
else w = ' 亿'
if (nums > 99999999 || nums < -99999999) {
nums = (nums / 10000).toFixed(2);
if (!ws) w = "<span style='font-size: 12px;'> 万亿</span>";
else w = ' 万亿'
}
}
}
// console.log(`nums2 -> ${nums}`);
if ((nums + "").indexOf(".") != -1) {
// 处理小数 截取获取小数点机器后的字符
ns = (nums + "").slice((nums + "").indexOf("."));
nums = Number((nums + "").slice(0, (nums + "").indexOf(".")));
} else {
nums = Number(nums);
}
// console.log(`nums3 -> ${nums} >>> ns -> ${ns} >>> w -> ${w}`);
let prefix = nums;
if (nums > 999999 || nums < -999999) {
var s1 = (nums + '').substring(0, (nums + '').length - 6);
var s2 = (nums + '').substring(s1.length, (nums + '').length - 3);
var s3 = (nums + '').substring((nums + '').length - 3);
prefix = s1 + ',' + s2 + ',' + s3;
} else if (nums > 999 || nums < -999) {
var s1 = (nums + '').substring(0, (nums + '').length - 3);
var s2 = (nums + '').substring((nums + '').length - 3);
prefix = s1 + ',' + s2;
}
return prefix + ns + w;
} else {
return nums;
}
}
// 校验只要是数字(包含正负整数,0以及正负浮点数)就返回true
function isNumber(val){
var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。
if(regPos.test(val) ) return true;
else return false;
}