How to do Decimal to float conversion in Python?

PythonType Conversion

Python Problem Overview

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

Python Solutions

Solution 1 - Python

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.

Solution 2 - Python

Just use float:

float_number = float(decimal_number)

Solution 3 - Python

Just cast it to float:

 number_in_float = float(number_you_have)


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnoobsterView Question on Stackoverflow
Solution 1 - PythonAmr MagdyView Answer on Stackoverflow
Solution 2 - PythonDanielView Answer on Stackoverflow
Solution 3 - PythonAhsanul HaqueView Answer on Stackoverflow