Integer vs Floating-Point Number
People think 3 and 3.0 are the same number to a computer, and they are not. An integer is a whole number, and its sums are exact. A floating-point number has a decimal part, and it keeps a very close value rather than an exact one. That is why 0.1 + 0.2 can give 0.30000000000000004. For money, count in whole cents with integers.
Integer
A whole number, like 7, 0 or -12, with nothing after the point.
An integer is a whole number with no part after the point. 7, 0 and -12 are integers, but 2.5 is not. Programs use integers for counting things, like items in a cart or the number of a page. Maths on integers is exact, so no tiny errors creep in. Most languages call the type int.
Floating-Point Number
A number with a decimal part, like 3.5. It can be off by a hair.
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.
In one line
- Integer
- A whole number, like 7, 0 or -12, with nothing after the point.
- Floating-Point Number
- A number with a decimal part, like 3.5. It can be off by a hair.