Skip to main content

How to Write a Decimal Number in Python with float

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A price of 19.99 needs a decimal point; a count of 3 doesn't. Python keeps the two kinds apart: whole numbers are int, numbers with a decimal point are float. This guide writes both in Python 3.15.2, checks their types, and shows the one surprise floats hold.

Syntax​

Syntax
<name> = <digits>.<digits>
PartWhat it means
<digits>.<digits>A number with a decimal point, such as 19.99 or 2.0
The decimal pointMakes it a float, even when nothing follows it but a zero

The Python tutorial says: "The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float."

The Example​

A shop line has a price of 19.99, a quantity of 3 and a weight of 2.0 kg. The script prints the type of each and the line's total.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

floats.py
price = 19.99
quantity = 3
weight = 2.0

print(type(price))
print(type(quantity))
print(type(weight))
print(price * quantity)

The floats.py script in a macOS-style code window: price is 19.99, quantity is 3 and weight is 2.0; the script prints the type of each, then price times quantity.

Step 2: Run It​

Terminal
uv run python floats.py
Output
<class 'float'>
<class 'int'>
<class 'float'>
59.97

The terminal after uv run python floats.py: class float, class int, class float, then 59.97.

19.99 is a float and 3 is an int. 2.0 is a float too: the decimal point decides, not whether there's anything after it. Multiplying a float by an int gave a float, 59.97. The command works unchanged in PowerShell.

Step 3: See That a Float Is Close, Not Exact​

float_sum.py
print(0.1 + 0.2)
print(7 / 2)
print(round(0.1 + 0.2, 2))
Terminal
uv run python float_sum.py
Output
0.30000000000000004
3.5
0.3

The terminal after uv run python float_sum.py: 0.30000000000000004, then 3.5, then 0.3.

  • 0.1 + 0.2 printed 0.30000000000000004. The floating-point chapter of the tutorial explains why: "the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine."
  • 7 / 2 gave the float 3.5: / always gives a float, even for two ints.
  • round(..., 2) rounded the sum to two decimal places for display: 0.3.

Common Mistakes​

MistakeWhat happensDo this instead
Comparing floats with ==, as in 0.1 + 0.2 == 0.3False, because of the tiny difference Step 3 showsCompare with a tolerance: math.isclose(0.1 + 0.2, 0.3) gave True
Writing 2 when you need a floatIt's an int; some libraries choose a different type from itWrite 2.0
Storing money as floats in accounting codeRounding differences add upUse decimal.Decimal or whole cents

FAQ​

How do I turn text into a float?​

float("19.99") gives 19.99.

What does 1e3 mean?​

It's 1000.0, a float written in scientific notation: e3 means "times ten to the power 3".

Conclusion​

19.99 and 2.0 are float and 3 is int. Floats are close approximations: 0.1 + 0.2 printed 0.30000000000000004, and round() gave 0.3 for display.

Additional Resources​