This is the function:
export const round = (value, precision) => {
const multiplier = Math.pow(10, precision || 0)
return Math.round(value * multiplier) / multiplier
}
When I use round
in this function:
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
}
I get when number is 0.4
:
1.4
1.7999999999999998
1.8
2.2
2.6
3
And when number is -0.4
(and starting from 3
):
2.6
2.2
1.8000000000000003
1.8
1.4
0.9999999999999999
Why am I getting these unrounded numbers and how to modify round
so I get 1.8
and 1
instead?
UPDATE: I tried solutions from other links. Like this one:
precision = precision || 0
return parseFloat(parseFloat(number).toFixed(precision))
I still get stuff like: 0.9999999999999999
.