Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Decimal to float conversion in Python?

I need to convert a Decimal value to float in Python. Is there any method available for this type conversion?

like image 906
noobster Avatar asked Aug 29 '15 12:08

noobster


People also ask

How do you convert to float in Python?

To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.

How do you add float and decimal in Python?

So if you have a number that could have as many as 15 decimal places you need to format as Decimal("%. 15f" % my_float) , which will give you garbage at the 15th decimal place if you also have any significant digits before decimal ( Decimal("%. 15f" % 100000.3) == Decimal('100000.300000000002910') ).

How do you convert decimals into Python?

Use the float() Function to Convert a String to Decimal in Python. Python provides an in-built function called float() that is utilized to transform a string or an integer to a floating-point value. Here, we will enter a string and perform the float() function on the given string.


1 Answers

There is two methods:

  • float_number = float ( decimal_number )
  • float_number = decimal_number * 1.0

First one using the built in function and second one without any function, but takecare about the 1.0 not 1 as it should be float so the result will be float.

like image 66
Amr Magdy Avatar answered Sep 20 '22 01:09

Amr Magdy