Rounding to 5 coefficient values ββin floating value
I want to convert a float value to round the value of 5 factors, that means for example 0.05,0.10,0.15
.
Suppose I have a value like this 9.48
and I want to convert it to 9.45
.
I tried with this:
val = 9.48 val - val % 0.05
Returns 9.450000000000001
. This is fine for me, but the problem is when I have 9.60
it converts asn to 9.55
.
When the value already has a factor of 5, it remains as it is.
source to share
This is the result where floating point numbers are represented - floats cannot represent exactly 9.60
. The closest (64-bit) float is 9.6
slightly smaller 9.6
, so the operation is rounded. If you want your math operations, then you should use a module decimal
, not float
.
eg.
val = decimal.Decimal("9.60")
val - val % decimal.Decimal("0.05")
See https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html for a detailed explanation of floating point math.
source to share