There is more than one way to convert string to double in Python. Its method has its own use if you understand them properly. Read on to find out your options when you need to have a double-precision representation of a string in Python.
Here is the sample code to convert string to double in python
from decimal import Decimal str1 = '1234.12' str2 = '14.12' # Solution1: Using float() to Convert String to Double in Python double1 = float(str1) print('Using float() to Convert String to Double in Python: ', double1) # Solution2: Using Decimal() to Convert String to Double in Python double2 = Decimal(str2) print('Using Decimal() to Convert String to Double in Python: ', double2)
Using float() to Convert String to Double in Python: 1234.12 Using Decimal() to Convert String to Double in Python: 14.12
Convert String To Double In Python
Double-precision Floating-point Numbers In Python
The most commonly used standard for floating-point arithmetic is IEEE-754, established by the Institute of Electrical and Electronics Engineers. In this standard, the double-precision data type is called double or binary64, which needs 64 bits to represent each value.
Many programming languages, such as Fortran, have officially implemented doubles. Their standards include strict requirements for this floating-point number type. Most computers and operating systems use it to present real numbers on their platforms as well.
Python doesn’t have an official definition for double-precision floating-point numbers. However, most implementations of this programming language (such as CPython – its de facto reference) map IEEE-754’s double to the float data type. Therefore, when you need to convert a string to a double-precision floating-point number in Python, you need to convert it from string to float type.
The function float() can return a floating-point number from a string or another number.
If you provide a string as the argument, that string should have a decimal number. It can be embedded in whitespace or preceded by a sign like ‘-‘ or ‘+’, though only the ‘-‘ sign has an effect on the resulting value. The string can also represent positive infinity, negative infinity, or not-a-number (NaN).
This is a simple example:
str = '20.22' # Convert String to Double in Python double = float(str)
First, we create a string object by assigning it to a literal representing a floating-point number. You can check its data type with the function type(). When you apply the function float() to it, the resulting object is now a float object.
We convert the string ‘Inf’ to a floating-point number with float(), and the result is ‘inf’ – the way Python represents positive infinity. The final statement finds the difference between infinity and a finite number, and as expected, the result is still infinity.
The decimal module comes with many improvements on floating-point arithmetic in Python. Compared to the built-in data type float, it has several advantages.
First and foremost, this module is designed with humans and what we learn at school in mind. Its developers aim to create a floating-point model as human-readable as possible. The module also represents decimal numbers exactly – one of the biggest weaknesses of the float type. This characteristic applies to arithmetic as well.
The expected result is zero, but we don’t get that due to the representation error of the float values created by the function float(). It means some decimal fractions can’t be exactly represented as binary fractions.
This isn’t the case with the decimal module. You can use its Decimal() function to exactly convert a string to a floating-point value. The results belong to the class decimal.Decimal.
from decimal import Decimal str = '1234.12' # Convert String to Double in Python double = Decimal(str)
As you can see, the addition above produces the exact result when we use the Decimal class instead of the default float data type.
Depending on your situation, you can use float() or decimal.Decimal() to convert string to double in Python. The former creates a regular float data type, while the latter produces an exact representation of floating-point numbers.