Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert float to fixed point decimal in python

I have some library function foo, that returns a floating point value with two decimal places (representing a price). I have to pass to to other function bar which expects a Decimal with fixed point for two decimal places.

value = foo() # say value is 50.15
decimal_value = decimal.Decimal(value) # Not expected. decimal_value contains Decimal('50.14999999999999857891452847979962825775146484375')
bar(decimal_value) # Will not work as expected

# One possible solution
value = foo() # say value is 50.15
decimal_value = decimal.Decimal(str(round(value,2))) # Now decimal_value contains Decimal('50.15') as expected
bar(decimal_value) # Will work as expected

Question:

How to convert arbitrary float to fixed Decimal point with 2 decimal places? And without intermediate string conversion using str.

I am not worried about the performance. Just want to confirm if intermediate str conversion is the pythonic way.

Update: Other Possible Solutions

# From selected answer
v = 50.15
d = Decimal(v).quantize(Decimal('1.00'))

# Using round (Does not work in python2)
d = round(Decimal(v), 2)
like image 900
balki Avatar asked Nov 02 '25 00:11

balki


1 Answers

Use Decimal.quantize:

Return a value equal to the first operand after rounding and having the exponent of the second operand.

>>> from decimal import Decimal
>>> Decimal(50.15)
Decimal('50.14999999999999857891452847979962825775146484375')
>>> Decimal(50.15).quantize(Decimal('1.00'))
Decimal('50.15')

Unlike the bad str approach, this works for any number:

>>> decimal.Decimal(str(50.0))
Decimal('50.0')
>>> decimal.Decimal(50.0).quantize(decimal.Decimal('1.00'))
Decimal('50.00')
like image 185


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!