|
|
@@ -21,28 +21,38 @@ export function getDecimalsNum(val: any, decimal = 2, maxCount = 6) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 强制保留小数位,零位四舍五入取整
|
|
|
- * @param val
|
|
|
- * @param decimal 需要保留小数位 默认2
|
|
|
+ * 强制保留小数位,不足位数自动补零
|
|
|
+ * @param value
|
|
|
+ * @param decimal 保留小数位
|
|
|
+ * @param round 是否四舍五入
|
|
|
* @returns
|
|
|
*/
|
|
|
-export function toDecimalFull(val: number, decimal = 2) {
|
|
|
+export function formatDecimal(value: number, decimal = 2, round = true) {
|
|
|
if (decimal <= 0) {
|
|
|
- return Math.round(val).toString();
|
|
|
+ if (round) {
|
|
|
+ return Math.round(value);
|
|
|
+ }
|
|
|
+ return Math.trunc(value);
|
|
|
} else {
|
|
|
- let str = val.toString();
|
|
|
- const num = str.indexOf('.');
|
|
|
- if (num < 0) {
|
|
|
+ if (round) {
|
|
|
+ return Number(value.toFixed(decimal));
|
|
|
+ }
|
|
|
+
|
|
|
+ let num = value.toString();
|
|
|
+ const index = num.indexOf('.');
|
|
|
+
|
|
|
+ if (index < 0) {
|
|
|
// 小数位自动补零
|
|
|
if (decimal > 0) {
|
|
|
- const count = str.length;
|
|
|
- str += '.';
|
|
|
- while (str.length <= count + decimal) {
|
|
|
- str += '0';
|
|
|
+ const count = num.length;
|
|
|
+ num += '.';
|
|
|
+ while (num.length <= count + decimal) {
|
|
|
+ num += '0';
|
|
|
}
|
|
|
}
|
|
|
- return str;
|
|
|
}
|
|
|
- return val.toFixed(decimal);
|
|
|
+
|
|
|
+ const result = num.substring(0, decimal + index + 1);
|
|
|
+ return Number(result);
|
|
|
}
|
|
|
-}
|
|
|
+}
|