2021-01-16 11:29:42 +08:00
|
|
|
import { isEmpty } from '@/utils/CommonUtils';
|
2020-12-23 11:14:35 +08:00
|
|
|
/*
|
|
|
|
* @Author: liqiang
|
|
|
|
* @Date: 2020-11-27 16:12:36
|
2021-01-16 11:29:42 +08:00
|
|
|
* @LastEditTime: 2021-01-06 10:01:28
|
2020-12-23 11:14:35 +08:00
|
|
|
* @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);
|
2021-01-16 11:29:42 +08:00
|
|
|
if(isEmpty(money)){
|
|
|
|
return money;
|
|
|
|
}
|
2020-12-23 11:14:35 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-01-16 11:29:42 +08:00
|
|
|
/**
|
|
|
|
* 根据传入的数字保留几位小数
|
|
|
|
* @param number 数字
|
|
|
|
* @param retain 小数点后保留几位
|
|
|
|
*/
|
|
|
|
export function enterTheNumberToRetainTheDecimal(number:any, retain:number){
|
|
|
|
if(isEmpty(number)){
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
let index = String(number).indexOf('.');
|
|
|
|
if(index === -1){
|
|
|
|
return number;
|
|
|
|
}else{
|
|
|
|
number = String(number).substring(0, index + (retain + 1));
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|