浏览代码

格式化小数位

li.shaoyi 3 年之前
父节点
当前提交
1350946b47

+ 3 - 3
src/common/components/echart/echart-timeline/index.vue

@@ -7,7 +7,7 @@ import { defineComponent, ref, watch, PropType, onMounted, computed } from 'vue'
 import { QueryTSDataRsp, QueryQuoteDayRsp } from '@/services/go/quote/interface';
 import { QueryTSData } from '@/services/go/quote';
 import { debounce, getRangeTime } from '@/utils/time';
-import { toDecimalFull } from '@/utils/number';
+import { formatDecimal } from '@/utils/number';
 import EchartBase from '../echart-base/index.vue';
 import { handleEchart } from './setup';
 import moment from 'moment';
@@ -121,8 +121,8 @@ export default defineComponent({
               // 计算总价
               const total = maIndexs.reduce((sum, val) => sum + data[val], 0);
               // 计算均线
-              const ma = toDecimalFull(total / count, decimal);
-              result.push(ma);
+              const ma = formatDecimal(total / count, decimal);
+              result.push(ma.toString());
             }
           }
         }

+ 3 - 3
src/common/components/echart/echart-timeline/setup.ts

@@ -1,5 +1,5 @@
 import { ref, watch } from "vue";
-import { toDecimalFull } from '@/utils/number';
+import { formatDecimal } from '@/utils/number';
 import { getTheme, ThemeEnum } from '@/common/config/theme';
 import { deepMerge } from '@/utils/objHandle'
 import { EChartsOption } from 'echarts';
@@ -63,7 +63,7 @@ export function handleEchart() {
         }
         if (yestclose > 0) {
             const result = (num - yestclose) / yestclose * 100;
-            return toDecimalFull(result) + '%';
+            return formatDecimal(result) + '%';
         }
         return '0%';
     }
@@ -143,7 +143,7 @@ export function handleEchart() {
                     min: min,
                     max: max,
                     axisLabel: {
-                        formatter: (val: number) => toDecimalFull(val, decimal),
+                        formatter: (val: number) => formatDecimal(val, decimal).toString(),
                     }
                 },
                 // Y轴右侧涨跌幅标签

+ 25 - 15
src/utils/number/index.ts

@@ -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);
     }
-}
+}