vue 使用Decimal解决精度问题, 根据两个值以及区间值,生成一个数组,满足0.1+0.2=0.3小数位的精度计算

引入Decimal.js

npm install --save decimal.js

import { Decimal } from 'decimal.js'

generateArray(startValue, endValue, interval) {
      var arr = [];
      // 确保起始值小于等于结束值
      if (startValue > endValue) {
        var temp = startValue;
        startValue = endValue;
        endValue = temp;
      }
      // 使用 Decimal.js 进行精确计算
      var decimalStart = new Decimal(startValue);
      var decimalEnd = new Decimal(endValue);
      var decimalInterval = new Decimal(interval);
      // 循环生成数组
      while (decimalStart.lte(decimalEnd)) {
        arr.push(decimalStart.toFixed(1)); 
        decimalStart = decimalStart.plus(decimalInterval);
      }
      return arr;
    },

你可能感兴趣的:(vue.js,javascript,前端)