How to Convert a String to a double in Python?

In this Python tutorial, we will learn how to convert a string to a double.

Table Of Contents

Convert a string to a float using float()

The float() is the direct method available in Python which will convert the specified string or integer to a float value. If we display the type of the converted variable, <class ‘float’=””> is returned.

Syntax:

float(string)

Parameter:

Advertisements

It takes only one parameter and returns a float value. Here the string represents the string value that has to be converted to float.

Example 1:

In this example, we will consider a string and convert it into a float value.

# Consider a string
strValue = '120.678'

print(strValue)

# Get the type of string1
print(type(strValue))

# Convert the string1 to float
floatValue = float(strValue)

# Get the type of converted
print(type(floatValue))

Output:

120.678
<class 'str'>
<class 'float'>

Firstly, the string – 120.678 belongs to the str class (string class). After converting into a float, the class type is float.

Example 2:

In this example, we will consider 5 strings in a list and convert them into float.

# Consider strings in a list
listOfStr = ['120.678','890.678','4.58','7.908','345.78897666']

print(listOfStr)

# Convert the strings to float
for strValue in listOfStr:
    # Get the type of converted
    floatValue = float(strValue)
    print(floatValue)
    print(type(floatValue))

Output:

['120.678', '890.678', '4.58', '7.908', '345.78897666']
120.678
<class 'float'>
890.678
<class 'float'>
4.58
<class 'float'>
7.908
<class 'float'>
345.78897666
<class 'float'>

Convert a string to a float using Decimal()

The Decimal() is the method available in the decimal module of Python. It converts the specified string or integer to a decimal value. If we display the type of the converted variable, <class ‘decimal.decimal’=””> is returned.

Syntax:

decimal.Decimal(string)

Parameter:
It takes only one parameter and returns the Decimal object. Here, the string represents the string value that has to be converted to decimal.

Example 1:

In this example, we will consider a string and convert it into a decimal value.

from decimal import Decimal

# Consider a string
strValue = '120.678'

print(strValue)

# Get the type of strValue
print(type(strValue))

# Convert the string to decimal
decimalValue = Decimal(strValue)

# Get the type of converted
print(type(decimalValue))

Output:

120.678
<class 'str'>
<class 'decimal.Decimal'>

Firstly, the string – 120.678 belongs to the str class (string class). After converting into decimal, the class type is decimal.Decimal.

Example 2:

In this example, we will consider 5 strings in a list and convert them into decimals.

from decimal import Decimal

# Consider strings in a list
strings = ['120.678','890.678','4.58','7.908','345.78897666']

print(strings)

# Convert the strings to decimal
for i in strings:
    print(Decimal(i))
    # Get the type of converted
    print(type(Decimal(i)))

Output:

['120.678', '890.678', '4.58', '7.908', '345.78897666']
120.678
<class 'decimal.Decimal'>
890.678
<class 'decimal.Decimal'>
4.58
<class 'decimal.Decimal'>
7.908
<class 'decimal.Decimal'>
345.78897666
<class 'decimal.Decimal'>

Summary

In this article, we have seen how to convert a string to a float or double using float() and Decimal() methods. You have to import the Decimal() method from the decimal module. Otherwise, it will return a no module found error. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top