js小数点精度问题

项目背景是用eharts 渲染数据,其中Y 轴的 刻度尺间隔用 interval,代码中如下:

yAxis: [
{
type : 'value',
position:'left',
min:minV,
max:maxV,
// splitNumber:5,
interval:interVal,
axisLine:{
lineStyle:{
color:'#fff'
}
},
axisLabel:{
show:true,
textStyle:{
color:'#6B7D97',
fontSize:'10'
},
formatter:function (value,index) {
return value.toFixed(4)
}
},
splitLine:{
show:true,
lineStyle:{
color:'#fff',
width:1
}
},
splitArea:{
show:true,
areaStyle:{
color:'#F6F8FB'
}
}
},
{
type : 'value',
position:'right',
min:-raa,
max:raa,
yAxisIndex:0,
splitNumber:8,
axisLine:{
lineStyle:{
color:'#fff'
}
},
axisLabel:{
show:true,
textStyle:{
color:'#6B7D97',
fontSize:'10'
},
formatter:function (value,index) {
return value.toFixed(2)+'%'
}
},
splitLine:{
show:true,
lineStyle:{
color:'#fff',
width:1
}
},
splitArea:{
show:true,
areaStyle:{
color:'#F6F8FB'
}
}
}
],

其中 interval 的小数点在RN项目中的小数点不能超过10位(没有具体测试),总之如果出现0.000346789999999这样的小数点的情况下会导致echarts图无法渲染,尝试用toFixed(6)这样处理的时候,
间隔的刻度不是我们想要的,因此采用的解决办法是保留小数点后8位,不能四舍五入,因此最终解决办法是:
0.000346789999999.toString().slice(0,8)用这样的处理方式,保留小数点的位数,同时不用四舍五入,即去尾法。
posted @ 2018-11-19 09:33  柠檬可乐  阅读(398)  评论(0编辑  收藏  举报