Skip to content
Home » How To Convert String to Double in Python

How To Convert String to Double in Python

Convert String to Double in Python

Good to hear, that you want to convert a string to a numeric variable with the precision equivalent to double type in Python. Python doesn’t have any double type variable but… float type has the same precision as double in other programming languages.

In this post, we will help you to convert strings to double by using the two methods that you can see below.

Let’s get started

Convert String to Double

  • Using float() method
  • Using decimal()

Process to use float() method:

It’s you know that the float() function accepts numbers & strings and returns the floating-point numbers as you can see in the output.

  • First, make a variable and give the float number.
  • Now make the second variable for converting
  • And just print()
  • so, let’s see what will return
whole = "10.02"
print("This is the initial string: " + whole)
 
# Converting to double
wholeblogs = float(whole)
print("The conversion of string to double is", wholeblogs)
 
wholeblogs = wholeblogs + 1
print("The converted string to double is incremented by 1:", wholeblogs)

Process to use decimal()  method:

As we told you above the decimal.Decimal() constructor, the decimal modules take a numeric value as input and convert it into a decimal type, it can have as much precision as needed that can exactly store and represent all the numeric values.

  • First, import it like this from decimal import Decimal.
  • Make a variable and give the float number.
  • Now make the second variable for converting
  • And just print()
from decimal import Decimal
 
 
whole = "10.02"
print("This is the initial string: " + whole)
 
# Converting to double
wholeblogs = Decimal(whole)
print("The conversion of string to double is", wholeblogs)
 
wholeblogs = wholeblogs+1
print("The converted string to double is incremented by 1:", wholeblogs)

Read More: Convert a list of dictionaries to pandas DataFrame

Tags:

Leave a Reply

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