🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

float()

AI & DATA SCIENCE // float

float() converts a compatible value — a numeric string or an int — into a floating-point number, or returns 0.0 when called with no arguments.

Syntax

float(x=0.0)

Deep Dive Course

float() parses strings containing decimal numbers, scientific notation, and even the special values 'inf' and 'nan' (case-insensitive). Passed an int, it simply adds a fractional part of .0. Like all binary floating-point implementations, Python floats can't represent every decimal value exactly, which is why adding 0.1 and 0.2 famously produces a result like 0.30000000000000004 instead of a clean 0.3.

1Understanding float()

float() parses strings containing decimal numbers, scientific notation, and even the special values 'inf' and 'nan' (case-insensitive). Passed an int, it simply adds a fractional part of .0. Like all binary floating-point implementations, Python floats can't represent every decimal value exactly, which is why adding 0.1 and 0.2 famously produces a result like 0.30000000000000004 instead of a clean 0.3.

💡

Never compare floats with == for 'closeness' — use math.isclose(a, b) instead, since rounding error accumulates in almost every non-trivial floating-point calculation.

editor.html
print(float("3.14"))
print(float("1e3"))
print(float(7))
localhost:3000

2Practical Example

Here is a real-world application of float() showing how it is used in production Python code.

editor.html
price_text = "19.99"
tax_rate = 0.08
price = float(price_text)
total = price * (1 + tax_rate)
print(f"Total: ${total:.2f}")
localhost:3000

3Best Practices

Follow these guidelines when working with float():

1. Use the decimal module instead of float for money or anywhere exact decimal precision matters

2. Use math.isclose() rather than == when comparing computed float results

3. Validate/catch ValueError when converting user input with float(), the same as with int()

⚠️

Tip: Never compare floats with == for 'closeness' — use math.isclose(a, b) instead, since rounding error accumulates in almost every non-trivial floating-point calculation.

editor.html
print(float("3.14"))
print(float("1e3"))
print(float(7))
localhost:3000

Examples

Example 01Basic Usage
print(float("3.14"))
print(float("1e3"))
print(float(7))
Example 02Advanced Example
price_text = "19.99"
tax_rate = 0.08
price = float(price_text)
total = price * (1 + tax_rate)
print(f"Total: ${total:.2f}")

Best Practices

  • Use the decimal module instead of float for money or anywhere exact decimal precision matters
  • Use math.isclose() rather than == when comparing computed float results
  • Validate/catch ValueError when converting user input with float(), the same as with int()

Interview Question

Why doesn't adding 0.1 and 0.2 give exactly 0.3 in Python, and how should you compare two floats for equality?

Hint: This is about binary floating-point representation, not a Python-specific bug.

Python floats are IEEE-754 double-precision binary numbers, and most decimal fractions, like 0.1, can't be represented exactly in binary, the same way one third can't be written exactly in decimal. The tiny representation error accumulates during arithmetic, so the sum comes out as 0.30000000000000004 instead of a clean value. To compare floats safely, use math.isclose(a, b) instead of ==, which allows for a small tolerance.

Exercises

MediumPractice using float() in a real scenario.
View Solution
print(float("3.14"))
print(float("1e3"))
print(float(7))

Frequently Asked Questions

Why doesn't adding 0.1 and 0.2 give exactly 0.3 in Python, and how should you compare two floats for equality?

Python floats are IEEE-754 double-precision binary numbers, and most decimal fractions, like 0.1, can't be represented exactly in binary, the same way one third can't be written exactly in decimal. The tiny representation error accumulates during arithmetic, so the sum comes out as 0.30000000000000004 instead of a clean value. To compare floats safely, use math.isclose(a, b) instead of ==, which allows for a small tolerance.

Related Functions

int()decimal.Decimalmath.isclose()