Convert float to rounded decimal equivalent

When you convert floatto Decimal, it Decimalwill contain as an exact representation of the binary number that it can. It's nice to be precise, but it's not always what you want. Since many decimal numbers cannot be represented exactly in binary format, the result Decimalwill be a little inactive - sometimes a little high, sometimes a little low.

>>> from decimal import Decimal
>>> for f in (0.1, 0.3, 1e25, 1e28, 1.0000000000001):
    print Decimal(f)

0.1000000000000000055511151231257827021181583404541015625
0.299999999999999988897769753748434595763683319091796875
10000000000000000905969664
9999999999999999583119736832
1.000000000000099920072216264088638126850128173828125

Ideally, we would like to Decimalround to the most probable decimal equivalent.

I tried converting to str, since a Decimalcreated from a string would be exact. Unfortunately, it strrounds too much.

>>> for f in (0.1, 0.3, 1e25, 1e28, 1.0000000000001):
    print Decimal(str(f))

0.1
0.3
1E+25
1E+28
1.0

Is there a way to get nicely rounded Decimalfrom a float?

+4
2

, repr a float , str. .

>>> for f in (0.1, 0.3, 1e25, 1e28, 1.0000000000001):
    print Decimal(repr(f))

0.1
0.3
1E+25
1E+28
1.0000000000001

, . , 15 - repr, , 1e25 1e28.

from decimal import Decimal,DecimalTuple

def _increment(digits, exponent):
    new_digits = [0] + list(digits)
    new_digits[-1] += 1
    for i in range(len(new_digits)-1, 0, -1):
        if new_digits[i] > 9:
            new_digits[i] -= 10
            new_digits[i-1] += 1
    if new_digits[0]:
        return tuple(new_digits[:-1]), exponent + 1
    return tuple(new_digits[1:]), exponent

def nearest_decimal(f):
    sign, digits, exponent = Decimal(f).as_tuple()
    if len(digits) > 15:
        round_up = digits[15] >= 5
        exponent += len(digits) - 15
        digits = digits[:15]
        if round_up:
            digits, exponent = _increment(digits, exponent)
    while digits and digits[-1] == 0 and exponent < 0:
        digits = digits[:-1]
        exponent += 1
    return Decimal(DecimalTuple(sign, digits, exponent))

>>> for f in (0.1, 0.3, 1e25, 1e28, 1.0000000000001):
    print nearest_decimal(f)

0.1
0.3
1.00000000000000E+25
1.00000000000000E+28
1.0000000000001

: . repr , float, . , , .

>>> print Decimal(repr(2.0/3.0))
0.6666666666666666
>>> print dec.nearest_decimal(2.0/3.0)
0.666666666666667

, repr, , , . nearest_decimal .

+4

Pharo Smalltalk Float asMinimalDecimalFraction.

, , float/double, ( ).

`. ` ?

0

Source: https://habr.com/ru/post/1502940/


All Articles