Python: Converting string into decimal number

Python: Converting string into decimal number

If you want the result as the nearest binary floating point number use float:

result = [float(x.strip( )) for x in A1]

If you want the result stored exactly use Decimal instead of float:

from decimal import Decimal
result = [Decimal(x.strip( )) for x in A1]

If you are converting price (in string) to decimal price then….

from decimal import Decimal

price = 14000,45
price_in_decimal = Decimal(price.replace(,,.))

No need for the replace if your strings already use dots as a decimal separator

Python: Converting string into decimal number

You will need to use strip() because of the extra bits in the strings.

A2 = [float(x.strip()) for x in A1]

Leave a Reply

Your email address will not be published. Required fields are marked *