-1

Have a look at the following code snippet and it's corresponding output.

>>> a = 0.01
>>> a
0.01
>>> type(a)
<class 'float'>
>>> from decimal import Decimal
>>> b = Decimal(a)
>>> b
Decimal('0.01000000000000000020816681711721685132943093776702880859375')
>>> type(b)
<class 'decimal.Decimal'>
>>> c = str(a)
>>> c
'0.01'
>>> type(c)
<class 'str'>
>>> d = Decimal(c)
>>> d
Decimal('0.01')
>>> type(d)
<class 'decimal.Decimal'

I am looking to convert float to decimal in my code but I am running into floating point errors. I am using decimal as a standard to maintain accuracy and precision.I am currently employing this hack of converting it to string and then decimal.

from typing import Optional
def float_to_decimal(float_value) -> Optional[Decimal]:
    try:
        if isinstance(float_value, float):
            logger.debug("entered value is of type float. :%f", float_value)
            decimal_value = Decimal(str(float_value))
        else:
            logger.debug("entered value is not of type float :%s", type(float_value))
            raise Exception()
    except Exception as e:
        logger.error("Error has occurred : %s %s", type(e), e)
        decimal_value = None
    finally:
        return decimal_value

Is there another cleaner way of doing this? My goal is to convert a float value of 0.01 to a decimal value of 0.01 without introducing any error.

float_value = 0.01
decimal_value = Decimal(float_value)

In the above lines of code, decimal_value takes a value of Decimal('0.01000000000000000020816681711721685132943093776702880859375') instead of Decimal('0.01')

  • Can you please clarify what you are trying to do? What is your desired output for some given input Note that the ``float`` literal ``0.01`` does not have the decimal value ``0.01``, just as no decimal can have the value 1/3. Any input converted to ``float`` is, by request, in ``float`` precision, not decimal precision. Converting it back to decimal cannot regain the precision. – MisterMiyagi Jun 23 '20 at 13:35
  • I've edited the question, accordingly – Harish Ranganathan Jun 23 '20 at 15:15

1 Answers1

-2

I am currently employing this hack of converting a float to string and then decimal.

from typing import Optional
def float_to_decimal(float_value) -> Optional[Decimal]:
    from decimal import Decimal
    try:
        if isinstance(float_value, float):
            logger.debug("entered value is of type float. :%f", float_value)
            decimal_value = Decimal(str(float_value))
        else:
            logger.debug("entered value is not of type float :%s", type(float_value))
            raise Exception()
    except Exception as e:
        logger.error("Error has occurred : %s %s", type(e), e)
        decimal_value = None
    finally:
        return decimal_value
  • This appears to be same code as in the question. Please clarify how it differs, or why it constitutes an answer. – MisterMiyagi Jun 23 '20 at 13:56
  • This has the same answer as that mentioned in the question, because converting floats to string and then to decimal doesn't seem to introduce floating point errors. My goal is to convert floats to decimal without introducing error. – Harish Ranganathan Jun 23 '20 at 15:17