Similar posts such as the following do not answer my question.
Convert a string to integer with decimal in Python
Consider the following Python code.
>>> import decimal
>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456') # How do I represent this as simply 23.456?
>>> d - 1
22 # How do I obtain the output to be 22.456?
How do I convert a string to a decimal number, so I am able to perform arithmetic functions on it and obtain an output with the correct precision?
If you want to stay in
decimalnumbers, safest is to convert everything:In Python 2.7, there’s an implicit conversion for integers, but not floats.