UNPKG

fenzhi-utils

Version:

分值前端项目的js函数库

74 lines (71 loc) 3.3 kB
/** * 千分位分隔符 * @param {number} num 被格式化的数字 * @param {number} decimalPlaces * @returns */ /* console.log(CustomThousandSplit(obj.a)); // "" console.log(CustomThousandSplit(obj.b)); // "" console.log(CustomThousandSplit(1000)); // "1,000" console.log(CustomThousandSplit(-1000)); // "-1,000" console.log(CustomThousandSplit(1234.5678, 2)); // "1,234.57" console.log(CustomThousandSplit(-1234.5678, 2)); // "-1,234.57" console.log(CustomThousandSplit(1234.5678)); // "1,234.5678" console.log(CustomThousandSplit(-1234.5678)); // "-1,234.5678" console.log(CustomThousandSplit(0, 2)); // "0" console.log(CustomThousandSplit(0, 2)); // "0.00" console.log(CustomThousandSplit(0.1234, 4)); // "0.1234" console.log(CustomThousandSplit(-0.1234, 4)); // "-0.1234" console.log(CustomThousandSplit(123456789)); // "123,456,789" console.log(CustomThousandSplit(-123456789)); // "-123,456,789" console.log(CustomThousandSplit(123456789.123456789, 5)); // "123,456,789.12346" console.log(CustomThousandSplit(-123456789.123456789, 5)); // "-123,456,789.12346" console.log(CustomThousandSplit(0.000000001, 9)); // "0.000000001" console.log(CustomThousandSplit(-0.000000001, 9)); // "-0.000000001" console.log(CustomThousandSplit(null)); // "" console.log(CustomThousandSplit(undefined)); // "" console.log(CustomThousandSplit('1000')); // "1,000" console.log(CustomThousandSplit('-1000')); // "-1,000" console.log(CustomThousandSplit('1234.5678', 2)); // "1,234.57" console.log(CustomThousandSplit('-1234.5678', 2)); // "-1,234.57" console.log(CustomThousandSplit('1234.5678')); // "1,234.5678" console.log(CustomThousandSplit('-1234.5678')); // "-1,234.5678" console.log(CustomThousandSplit('0')); // "0" console.log(CustomThousandSplit('0.1234', 4)); // "0.1234" console.log(CustomThousandSplit('-0.1234', 4)); // "-0.1234" console.log(CustomThousandSplit('123456789')); // "123,456,789" console.log(CustomThousandSplit('-123456789')); // "-123,456,789" console.log(CustomThousandSplit('123456789.123456789', 5)); // "123,456,789.12346" console.log(CustomThousandSplit('-123456789.123456789', 5)); // "-123,456,789.12346" console.log(CustomThousandSplit('0.000000001', 9)); // "0.000000001" console.log(CustomThousandSplit('-0.000000001', 9)); // "-0.000000001" */ export function CustomThousandSplit(num, decimalPlaces) { if ([null, undefined].includes(num)) { return ''; } num = String(num); const thousandSeparator = ','; const decimalSeparator = '.'; const sign = num[0] === '-' ? '-' : ''; const absNum = Math.abs(parseFloat(num)); const integerPart = Math.floor(absNum) .toString() .replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); const decimalPart = decimalPlaces !== undefined ? decimalSeparator + absNum.toFixed(decimalPlaces).split('.')[1] : ''; return sign + integerPart + decimalPart; } // export function numberFormat(num) { // if (isNaN(parseFloat(num)) || parseFloat(num) === Infinity) return ''; // let fixedIndex = // num.toString().indexOf('.') === -1 // ? 0 // : num.toString().split('.')[1].length; // return parseFloat(num) // .toFixed(fixedIndex) // .replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'); // }