Describe the bug
Even with ROUND_HALF_UP, some pre-rounded numbers will not work well.
The recommendation is not rounding several times during a production line, but it still happens.
To Reproduce
import numpy as np
from decimal import ROUND_HALF_UP, Decimal, localcontext
import pandas as pd
x = np.float64(103.55)
with localcontext() as ctx:
ctx.rounding = ROUND_HALF_UP
result = round(Decimal(x), 1)
result
Expected behaviour
np.float64(103.55) with ROUND_HALF_UP to one decimal place is expected to be 103.6 nor 103.5.
This happens because the actual np.float is 103.54999999...
A possible "hack" around this is using the str() version of the number. But this might be dangerous business dependant on the datatype of the float...
Because of the extra str(), this gives 103.6
import numpy as np
from decimal import ROUND_HALF_UP, Decimal, localcontext
import pandas as pd
x = np.float64(103.55)
with localcontext() as ctx:
ctx.rounding = ROUND_HALF_UP
result = round(Decimal(str(x)), 1)
result
Platforms and Environments
DaplaLab with Jupyter
Version
1.4.0
Error messages or logs
Describe the bug
Even with ROUND_HALF_UP, some pre-rounded numbers will not work well.
The recommendation is not rounding several times during a production line, but it still happens.
To Reproduce
Expected behaviour
np.float64(103.55) with ROUND_HALF_UP to one decimal place is expected to be 103.6 nor 103.5.
This happens because the actual np.float is 103.54999999...
A possible "hack" around this is using the str() version of the number. But this might be dangerous business dependant on the datatype of the float...
Because of the extra str(), this gives 103.6
Platforms and Environments
DaplaLab with Jupyter
Version
1.4.0
Error messages or logs