绩效考核手机版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
886 B

3 years ago
/**
* @description: 格式化数字
* @param {Number} number 数字
* @param {Number} places 在有小数时保留的小位数默认0位
* @param {String} thousand 用啥隔开','
* @param {String} decimal 表示小数点:'.'
* @return {String}
* @example formatNumber(12345) // 12,345
*/
export default function formatNumber(number = 0, places = 0, thousand = ',', decimal = '.') {
number = +number || 0;
const negative = number < 0 ? '-' : '';
const i = parseInt(Math.abs(number)) + '';
const j = i.length > 3 ? i.length % 3 : 0;
const decimalIndex = ('' + number).lastIndexOf('.');
return negative +
(j ? i.substr(0, j) + thousand : '') +
i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) +
(
places > 0 && decimalIndex > -1
? decimal + ('' + number).substr(decimalIndex + 1, places).padEnd(places, '0')
: ''
);
}