In this chapter
We'll see how computers store whole numbers and decimals — signed and unsigned integers, why every integer has a maximum size, and why 0.1 + 0.2 doesn't equal exactly 0.3 — and what that means for prices.
The Problem in Real Life
While testing a fix, Anna adds up two discounts in the code: 0.1 and 0.2. She expects 0.3. The screen prints 0.30000000000000004.
She checks it three times, then calls John over, half-laughing. "Either I broke maths, or the computer did." John smiles. "Neither. And on a ticketing site, this is a very important thing to understand — because these numbers are people's money."
Computers are very good with numbers. Just not always the numbers you think they're storing.
John
Numbers on Paper vs. Numbers in Memory
Decimals aren't always exact
Some simple decimals like 0.1 can't be stored perfectly in binary.
Every number has a size limit
A number is stored in a fixed number of bits, so it has a biggest possible value.
Money must be exact
Tiny rounding errors are fine in a game, but not in someone's bank payment.
How Computers Store Numbers
Computers store two main kinds of numbers: integers (whole numbers like 7, 0 or -42) and floating-point numbers (numbers with a decimal point, like 49.99). They are stored in very different ways.
- Integers have a fixed size. An integer is stored in a fixed number of bits — often 8, 16, 32 or 64. More bits means bigger numbers. For example, 8 bits can hold 256 different values.
- Unsigned integers can only be zero or positive. With 8 bits, that's 0 to 255. Good for things that are never negative, like a count of seats.
- Signed integers can be negative too. One bit is effectively used for the sign, so 8 bits hold -128 to 127. (Computers use a clever method called two's complement for this, but you don't need its details now.)
- Overflow — if a number grows past the biggest value its bits can hold, it "wraps around", like a car's odometer rolling back to 0000. This has caused real bugs: in 2014, YouTube's view counter used a 32-bit signed integer (max about 2.1 billion), and the video "Gangnam Style" got so many views that YouTube had to switch to 64-bit numbers.
- Floating-point numbers store decimals in a kind of scientific notation in binary (a number times a power of 2). This lets them hold huge and tiny values, but most decimals can only be stored approximately.
| Size | Unsigned range | Signed range |
|---|---|---|
| 8 bits | 0 to 255 | -128 to 127 |
| 16 bits | 0 to 65,535 | -32,768 to 32,767 |
| 32 bits | 0 to about 4.29 billion | about -2.1 billion to 2.1 billion |
| 64 bits | 0 to about 18 quintillion | about ±9.2 quintillion |
Choosing too small a size causes overflow; most modern code uses 32 or 64 bits.
| Approach | Stored value | Exact? |
|---|---|---|
| Floating-point dollars | 49.99 (stored as 49.9899999...) | No — tiny errors can add up |
| Integer cents | 4999 | Yes — always exact |
| Decimal type (database) | 49.99 | Yes — made for money |
Run these lines in your browser's console (press F12 and open the Console tab).
0.1 + 0.2 // 0.30000000000000004// The safe way for money: work in centsconst priceCents = 4999; // 49.99const totalCents = priceCents * 3; // 14997(totalCents / 100).toFixed(2) // "149.97"
The rounding only happens at the very end, when the number is shown to a person.
Why is 0.1 + 0.2 not 0.3? In decimal, you can't write 1/3 exactly — it's 0.3333... forever, so you have to round somewhere. Binary has the same problem with 1/10. The number 0.1 in binary goes on forever (0.000110011001100...), so the computer stores the closest value it can. Add two slightly-rounded numbers, and you get a slightly-rounded answer: 0.30000000000000004. This happens in almost every programming language, because almost all of them use the same standard for floating-point numbers (IEEE 754).
What this means for BlueTicket. Prices must be exact. If a ticket costs 49.99 and you add many of them up as floating-point numbers, tiny errors can build up until a total is off by a cent. The standard solution: store money as whole numbers of the smallest unit — cents, not dollars. 49.99 becomes the integer 4999. Integers are always exact. You only turn it back into "49.99" when showing it on screen. Databases and some languages also offer special exact decimal types for money.
Anna checks the pricing code. It already stores prices in cents — someone learned this lesson before her. Her discount test was the only place using floats. She changes it, and the total comes out exactly right.
Key Takeaway
Integers are stored exactly but have a maximum size set by their number of bits; floating-point numbers can hold decimals but most of them only approximately. That's why money should be stored as whole numbers of cents, never as floating-point values.
Why This Matters
Number bugs are some of the most expensive bugs in software, because they quietly produce wrong totals instead of crashing. As BlueTicket grows, Anna will build discounts, refunds and reports — and every one of them has to add up to the exact cent. Knowing the difference between integers and floats is how she'll avoid charging 50,000 fans the wrong amount on Sale Day.
Numbers make sense now. Time to go back to Zoë's name — because letters, it turns out, are stored as numbers too. And that's exactly where her name went wrong.
