Skip to content

_round produces some unexpected results on certain numbers #266

Description

@aecorn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions