|
@@ -1,3 +1,4 @@
|
|
|
+import { multiply,divide } from '@/utils/calculate';
|
|
|
// 字母列标
|
|
|
export function getColumnHeaderCode(len) {
|
|
|
let tag_arr = [];
|
|
@@ -271,15 +272,26 @@ export function isNumberVal(value) {
|
|
|
}
|
|
|
|
|
|
/* 增加减少小数点位数 */
|
|
|
-export function transDecimalPlace(str,decimalNum) {
|
|
|
- console.log(str,decimalNum)
|
|
|
- let s = str.replace(/%/,''), decimalPlaces = getDecimalPlaces(str);
|
|
|
+export function transDecimalPlace(str,{pn,nt}) {
|
|
|
+ let s = str.replace(/%/,''),
|
|
|
+ decimalPlaces = getDecimalPlaces(str),
|
|
|
+ decimalNum=pn;
|
|
|
+
|
|
|
+ //是否是设置百分比后的
|
|
|
+ let transPercent = nt==='percent' ? true : false;
|
|
|
|
|
|
//后缀 百分号
|
|
|
- let suffix = str.endsWith('%') ? '%' : '';
|
|
|
+ let suffix = (str.endsWith('%')||transPercent) ? '%' : '';
|
|
|
|
|
|
let num = parseFloat(s);
|
|
|
+
|
|
|
if(decimalPlaces===0) { //整数
|
|
|
+ if(transPercent) {
|
|
|
+ return decimalNum > 0
|
|
|
+ ? `${multiply(num,100)}.${'0'.repeat(decimalNum)}${suffix}`
|
|
|
+ : `${multiply(num,100)}${suffix}`;
|
|
|
+ }
|
|
|
+
|
|
|
// 补零
|
|
|
return decimalNum > 0
|
|
|
? `${s}.${'0'.repeat(decimalNum)}${suffix}`
|
|
@@ -290,30 +302,19 @@ export function transDecimalPlace(str,decimalNum) {
|
|
|
// decimalStr = str.split('.')[1];
|
|
|
|
|
|
if(decimalNum > 0) {
|
|
|
- return `${s}${'0'.repeat(decimalNum)}${suffix}`
|
|
|
+ let addPointStr = `${s}${'0'.repeat(decimalNum)}`;
|
|
|
+
|
|
|
+ return transPercent
|
|
|
+ ? `${parseFloat(multiply(num,100)).toFixed(decimalPlaces+decimalNum-2)}${suffix}`
|
|
|
+ : `${addPointStr}${suffix}`
|
|
|
}else {
|
|
|
let maxDecimal = Math.max(0,decimalNum+decimalPlaces);
|
|
|
- return `${parseFloat(num.toFixed(maxDecimal))}${suffix}`
|
|
|
- }
|
|
|
-
|
|
|
- // if(decimalPlaces===0) { //整数
|
|
|
- // // 补零
|
|
|
- // const zerosToAdd = Math.max(0, decimalNum);
|
|
|
- // return zerosToAdd > 0
|
|
|
- // ? `${s}.${'0'.repeat(zerosToAdd)}${suffix}`
|
|
|
- // : `${s}${suffix}`;
|
|
|
- // }
|
|
|
+ let maxDecimalPercent = Math.max(0,maxDecimal-2);
|
|
|
|
|
|
- // let integerStr = str.split('.')[0],
|
|
|
- // decimalStr = str.split('.')[1];
|
|
|
-
|
|
|
- // if(decimalNum>0) {
|
|
|
- // return `${s}${'0'.repeat(decimalNum)}${suffix}`
|
|
|
- // } else {
|
|
|
- // return decimalNum+decimalPlaces > 0
|
|
|
- // ? `${integerStr}.${decimalStr.substring(0,decimalNum+decimalPlaces)}${suffix}`
|
|
|
- // : `${integerStr}${suffix}`
|
|
|
- // }
|
|
|
+ return transPercent
|
|
|
+ ? `${parseFloat(multiply(num,100)).toFixed(maxDecimalPercent)}${suffix}`
|
|
|
+ : `${parseFloat(num.toFixed(maxDecimal))}${suffix}`
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/* 计算小数点位数 */
|
|
@@ -336,7 +337,7 @@ export function getDecimalPlaces(numStr) {
|
|
|
export function transNumPercentType(str,type) {
|
|
|
console.log(str,type)
|
|
|
|
|
|
- let isPercent = str.endsWith('%'),s=str.replace(/%/,''),decimalPlaces = getDecimalPlaces(str);
|
|
|
+ let isPercent = str.endsWith('%'),s=str.replace(/%/,'');
|
|
|
|
|
|
if(isPercent && type==='percent') { //百分数转百分
|
|
|
return str
|
|
@@ -349,19 +350,9 @@ export function transNumPercentType(str,type) {
|
|
|
let num = parseFloat(s)
|
|
|
|
|
|
if(type==='percent') {
|
|
|
- const percenStr = (num * 100).toFixed(2) + '%';
|
|
|
+ const percenStr = parseFloat(multiply(num,100)) + '%';
|
|
|
return percenStr;
|
|
|
}else {
|
|
|
- return (num/100).toFixed(2);
|
|
|
+ return parseFloat(divide(num,100));
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-export function transValueFormat(str,{nt,pn}) {
|
|
|
- let str1 = '',str2 = '';
|
|
|
-
|
|
|
- str1 = nt ? transNumPercentType(str,nt) : str;
|
|
|
-
|
|
|
- str2 = pn!==0 ? transDecimalPlace(str1,pn) : str1;
|
|
|
-
|
|
|
- return str2;
|
|
|
}
|