Python: Converting string into decimal number

PythonStringFloating PointDecimal

Python Problem Overview


I have a python list with strings in this format:

A1 = [' "29.0" ',' "65.2" ',' "75.2" ']

How do I convert those strings into decimal numbers to perform arithmetic operations on the list elements?

Python Solutions


Solution 1 - Python

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]

Solution 2 - Python

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

Solution 3 - Python

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

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

Solution 4 - Python

use the built in float() function in a list comprehension.

A2 = [float(v.replace('"','').strip()) for v in A1]

Solution 5 - Python

A2 = [float(x.strip('"')) for x in A1] works, @Jake , but there are unnecessary 0s

Solution 6 - Python

In Python there are two floating point datatypes Float and Decimal. The use case depends on the precision of decimal you want in your program. Float is quick and Decimal is precise.

To convert a string to a floating point number just do

import decimal
float('1.2')
decimal.Decimal('1.2')

Analysing Float and Decimal objects

>>> import sys
>>> from decimal import Decimal

>>> num = '1.2'

>>> float(num)
1.2

>>> Decimal(num)
Decimal('1.2')


>>> # Precision
>>> float(1.2)
1.2

>>> Decimal(1.2)
Decimal('1.1999999999999999555910790149937383830547332763671875')


>>> # Memory usage
>>> sys.getsizeof(Decimal(num))
104

>>> sys.getsizeof(float(num))
24


>>> # Performance
>>> %timeit float(num)
140 ns ± 2.27 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

>>> %timeit Decimal(num)
192 ns ± 9.42 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In your particular case you can use @Mark Byers solution.

Solution 7 - Python

There are two floating-point data types. Float and Decimal. It depends on your program demand which one you want to use. Decimal provides a high level of accuracy and it's more precise than Float.

To convert a value string to float just do it:

num = "29.0"
print (float(num))

To convert string to decimal

from decimal import Decimal
num = "29.0"
print (Decimal(num))

For your specific question, you can use the below code. Both are working for me.

from decimal import Decimal
import re

result = [float(x.strip(' "')) for x in A1]
print(result)
#[29.0, 65.2, 75.2]


result = [Decimal(x.strip(' "')) for x in A1]
print(result)
#[Decimal('29.0'), Decimal('65.2'), Decimal('75.2')]


result = [float(re.search(r'\d+.\d+',number).group()) for number in A1]
print(result)
#[29.0, 65.2, 75.2]

result = [Decimal(re.search(r'\d+.\d+',number).group()) for number in A1]
print(result)
#[Decimal('29.0'), Decimal('65.2'), Decimal('75.2')]

Solution 8 - Python

If you are converting string to float:

import re
A1 = [' "29.0" ',' "65.2" ',' "75.2" ']
float_values = [float(re.search(r'\d+.\d+',number).group()) for number in A1]
print(float_values)
>>> [29.0, 65.2, 75.2]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSankar AView Question on Stackoverflow
Solution 1 - PythonMark ByersView Answer on Stackoverflow
Solution 2 - PythonNids BarthwalView Answer on Stackoverflow
Solution 3 - PythonJakeView Answer on Stackoverflow
Solution 4 - PythonfarzadView Answer on Stackoverflow
Solution 5 - PythontekknolagiView Answer on Stackoverflow
Solution 6 - PythonAll Іѕ VаиітyView Answer on Stackoverflow
Solution 7 - PythonRuhul AminView Answer on Stackoverflow
Solution 8 - PythonPradamView Answer on Stackoverflow