引入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;
},