Skip to content
BENIBIX Learn

Floating-Point Number

Development🧩 Programming ConceptsData and Variables

Also known as Float, Decimal Number, Double

A number with a decimal part, like 3.5. It can be off by a hair.

Think of it like

Think of it like a ruler with very fine marks. It is close for nearly any length, but not always spot on.

Example

A fitness app stores a 5.2 km run as a float. For the price of the plan it uses 999 cents, not 9.99.

Why it matters

Distances, weights and scores often have decimals. Knowing the tiny error keeps you from using floats for money.

Definition

A floating-point number is a number with a decimal part, like 3.5. Computers store these in a fixed number of bits, so most of them are kept as a very close guess. That is why 0.1 + 0.2 can come out as 0.30000000000000004. The tiny error is fine for a game or a map. For money, programs use whole cents or a special decimal type instead.

Example code

python
>>> 0.1 + 0.2
0.30000000000000004
The answer is a hair off, because the computer stores a close guess.

Common mistake

Storing prices as floats. Small errors add up, and a total can end one cent off.

Key takeaways

  1. 1
    Has a decimal part. Holds values like 3.5 and 0.25.
  2. 2
    Close, not exact. Most values are a very near guess.
  3. 3
    Not for money. Use whole cents or a decimal type.

Compare

Learn it properly