# Decimal

> The Decimal is the number system most commonly used by humans, based on ten digits (0-9).

## Why is 0.1 + 0.2 not equal to 0.3 in JavaScript?

The base-10 number system, which uses ten digits (0 to 9). It's the most familiar system to humans for counting and arithmetic.

- Topic: Number Systems
- Type: concept, format

## Example

Wrong:

```js
const total = 0.1 + 0.2;
console.log(total === 0.3); // false
```

Right:

```js
const totalCents = 10 + 20;
console.log(totalCents === 30); // true
```

Binary floats can't store most decimal fractions exactly, so money is kept in integer cents.

## Common mistakes

- Storing prices as floats and getting totals like 0.30000000000000004.

## Key points

- Money is stored as integer minor units, like cents, or a decimal type, not binary floats.
- Floats are compared with a tolerance like `Number.EPSILON`, not `===`.
- Money columns in SQL use `DECIMAL` or `NUMERIC`, not `FLOAT`.

## Interview questions

### Why can't computers store 0.1 exactly?

Numbers are stored in binary, and 0.1 has an infinite binary fraction, so it is rounded to the closest value that fits.

### Should I use `toFixed` to fix float errors?

Only for display. `toFixed` returns a rounded string; do the math in integers or a decimal library.

## Related cards

- [Binary](https://wonderium.dev/engineering/binary.md)
- [Hexadecimal](https://wonderium.dev/engineering/hexadecimal.md)
- [Number.EPSILON](https://wonderium.dev/js/number-epsilon.md)
- [.toFixed()](https://wonderium.dev/js/number-to-fixed.md)
- [Intl.NumberFormat](https://wonderium.dev/js/intl-number-format.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/decimal
- Last updated: 2026-11-25
