<分区>
这是函数:
export const round = (value, precision) => {
const multiplier = Math.pow(10, precision || 0)
return Math.round(value * multiplier) / multiplier
}
当我在此函数中使用 round
时:
SET_CAMERA_ZOOM (state, number = 0) {
// the default is 1.4
const cameraZoom = state.markerEditor.cameraZoom
const roundedCameraZoom = round(cameraZoom, 1)
state.markerEditor.cameraZoom = roundedCameraZoom + number
}
当数字为 0.4
时我得到:
1.4
1.7999999999999998
1.8
2.2
2.6
3
当数字为 -0.4
(从 3
开始)时:
2.6
2.2
1.8000000000000003
1.8
1.4
0.9999999999999999
为什么我得到这些未舍入的数字以及如何修改 round
以便我得到 1.8
和 1
?
更新:我尝试了来自其他链接的解决方案。就像这个:
precision = precision || 0
return parseFloat(parseFloat(number).toFixed(precision))
我仍然得到像这样的东西:0.9999999999999999
。