12-23-上传master
This commit is contained in:
37
src/utils/NumberUtils.ts
Normal file
37
src/utils/NumberUtils.ts
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* @Author: liqiang
|
||||
* @Date: 2020-11-27 16:12:36
|
||||
* @LastEditTime: 2020-11-27 16:21:29
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @Description: 数字工具类
|
||||
* @FilePath: \ebtp-cloud-frontend\src\utils\NumberUtils.ts
|
||||
*/
|
||||
/**
|
||||
* 金额正则
|
||||
*/
|
||||
const amountReplace = /(\d)(?=(\d{3})+(?!\d))/g;
|
||||
|
||||
function getZero(num: number) {
|
||||
let number = '';
|
||||
for (let i = 0; i < num; i++) {
|
||||
number += '0';
|
||||
}
|
||||
return number;
|
||||
}
|
||||
/**
|
||||
* 数字转换金额格式
|
||||
* @param money 数字
|
||||
* @param retain 小数点后保留几位
|
||||
*/
|
||||
export function digitalConversionAmount(money: string, retain: number) {
|
||||
let zero = getZero(retain);
|
||||
if (money.indexOf('.') === -1) {
|
||||
money = money.replace(amountReplace, '$1,') + '.' + zero;
|
||||
} else {
|
||||
let division = money.split('.');
|
||||
money = division[0].replace(amountReplace, '$1,');
|
||||
money = money + '.' + (division[1] + zero).substr(0, retain);
|
||||
}
|
||||
return money;
|
||||
};
|
||||
|
Reference in New Issue
Block a user