Convert the string to Double in Python
In this tutorial, we are going to learn about how to convert the string to a double in Python with the help of examples.
Consider, we have the following string in our code:
str = "234.123"
Now, we need to convert the above string "234.123"
to a double 234.123
.
Using float() function
To convert a string to a double, we can use the built-in float()
function in Python.
The float()
function takes the string as an argument and converts it to a double.
Here is an example:
str = "234.123"
print(float(str))
Output:
234.123
Similarly, we can use the Decimal()
function in Python to convert a string to Double.
Here is an example:
from decimal import Decimal
str = "234.123"
result = Decimal(str)
print(result)
print(type(result))
Output:
234.123
<class 'decimal.Decimal'>
Note: In the above code, we have imported the Decimal()
function from the decimal
module.