A float is a data type that holds numbers with decimal points

In programming, a float is a way to store numbers that have a fractional part — anything with a decimal point. When you write 3.14, 0.5, or 99.99 in code, you are working with a float. The word "float" comes from "floating-point," which describes how the computer represents these numbers internally by moving the decimal point around.

Floats are different from integers, which are whole numbers only (1, 42, -7). If you need to store a temperature like 98.6 degrees, a price like $19.99, or a percentage like 87.5%, you use a float. Most programming languages — Python, Java, C, JavaScript — have a float type built in.

The trade-off is that floats take up more memory than integers and can sometimes give you unexpected results due to how computers store them in binary. A float might store 0.1 as something very close to 0.1 but not exactly 0.1, which matters when you are doing precise calculations.

Key Takeaways

  • A float stores numbers with decimal points, while an integer stores only whole numbers.
  • Floats use more memory than integers but let you represent fractional values like prices, temperatures, and percentages.
  • Different programming languages have different float sizes: some use 32 bits, others use 64 bits, which affects how many decimal places they can store accurately.
  • Floats can produce rounding errors in calculations because computers represent decimals in binary, not decimal form.

How floats differ from integers and other number types

An integer holds a whole number: 5, -12, 0. A float holds a decimal: 5.5, -12.3, 0.0. If you try to store 5.5 in an integer variable, most languages will either round it down to 5 or throw an error, depending on the language.

Some languages also have a double type, which is a float that uses twice as much memory (usually 64 bits instead of 32). A double can store more decimal places and larger numbers more accurately. In Python, there is no separate double type — the float type is always double-precision. In Java and C, you choose between float and double depending on how precise you need to be.

There is also the decimal type in some languages like Python and C#, which stores numbers as exact decimal values rather than approximations. Decimals are slower and use more memory than floats, but they are useful for financial calculations where rounding errors matter.

Why computers store floats the way they do

Computers store floats using a format called IEEE 754, which represents a number as three parts: a sign (positive or negative), a mantissa (the significant digits), and an exponent (which moves the decimal point). This is similar to scientific notation: 1.5 × 10² is 150.

This system lets computers store very large numbers (like 1.7 × 10³⁸) and very small numbers (like 1.4 × 10⁻⁴⁵) in the same amount of space. The downside is that not every decimal number can be represented exactly. The number 0.1 cannot be written perfectly in binary, so the computer stores the closest approximation it can.

This is why adding 0.1 + 0.2 in many languages gives you 0.30000000000000004 instead of 0.3. The error is tiny, but it exists. For everyday calculations it does not matter, but for financial software or scientific work, you need to be aware of it.

When to use a float in your code

Use a float when you need to store any number with a decimal point. Common examples are prices ($19.99), measurements (5.5 inches), percentages (87.3%), and scientific values (the speed of light is 299,792,458 meters per second, but you might store 2.998 × 10⁸ as a float).

Use an integer when you are counting things that cannot be fractional: the number of items in a shopping cart, the number of users logged in, or the year. Integers are faster and use less memory, so if you do not need decimals, do not use a float.

If you are doing financial calculations, consider using a decimal type instead of a float. Banks and payment systems use decimals or integers (storing cents instead of dollars) to avoid rounding errors that could cost money.

Common mistakes when working with floats

The most common mistake is comparing floats with the equals operator. If you write if (x == 0.1), you might get false even when x should be 0.1, because of rounding errors. Instead, check whether the difference is smaller than a tiny threshold: if (abs(x - 0.1) < 0.0001).

Another mistake is assuming a float can store unlimited decimal places. A 32-bit float has about 7 significant digits of precision. A 64-bit double has about 15. If you need more precision, you need a decimal type or a special library.

A third mistake is mixing floats and integers in calculations without thinking about the result. In most languages, dividing two integers gives an integer result: 5 / 2 = 2, not 2.5. If you want 2.5, you need to convert one of them to a float first: 5.0 / 2 = 2.5.

How different languages handle floats

Python treats floats straightforward: there is one float type, and it is always 64-bit. You write 3.14 and Python knows it is a float. Python also has the decimal module if you need exact decimal arithmetic.

Java requires you to choose: float (32-bit) or double (64-bit). You declare the type when you create the variable: float price = 19.99f; or double price = 19.99;. The f at the end tells Java it is a float, not a double.

JavaScript has only one number type, which is always a 64-bit float. This means 5 and 5.0 are the same thing to JavaScript, and even integers are stored as floats internally.

C and C++ let you use float or double, and you must declare which one. C also has long double for even more precision on some systems.

Frequently Asked Questions

Why is it called "floating point"?

The decimal point "floats" because the exponent part moves it around. In scientific notation, 1500 can be written as 1.5 × 10³ or 15 × 10², and the decimal point is in a different place each time. Computers use the same idea internally to store numbers of very different sizes in the same amount of memory.

Can I convert a float to an integer?

Yes. Most languages let you cast or convert a float to an integer, which drops the decimal part. Converting 5.9 to an integer gives 5, not 6. Some languages round instead of truncating, so check your language's documentation. You can also use rounding functions like round(), floor(), or ceil() to control what happens.

What is the difference between float and double?

A float is usually 32 bits and can store about 7 significant digits accurately. A double is usually 64 bits and can store about 15 significant digits. Use double when you need more precision or are working with very large or very small numbers. For most everyday code, double is the safer choice.

Why do I get weird numbers when I add floats together?

Floats are stored in binary, and many decimal numbers cannot be represented exactly in binary. When you add floats, the small rounding errors can add up. This is normal and expected. For financial calculations, use a decimal type instead, or store money as integers (cents) and divide by 100 when you display it.

Should I always use double instead of float?

In most modern code, yes. Memory is cheap, and double gives you better precision with fewer surprises. Float is mainly useful when you are working with very large datasets where memory matters, or when you are interfacing with hardware or older systems that expect 32-bit floats.