Money is not stored with decimals
Floating-point numbers (float/double) do not represent decimals
exactly. The classic example:
0.1 + 0.2; // 0.30000000000000004 ❌
In a store, that error piles up invoice after invoice and ends up throwing the accounting off. The professional rule is to store money as an integer in its smallest unit: cents.
- $25.00 is stored as
2500. - $149.99 is stored as
14999.
Integers add and multiply without error. Only when displaying the price do you divide by 100 to render the dollars:
(14999 / 100).toFixed(2); // "149.99"
Stock control
stock is the available quantity. Before accepting a purchase you must
check that there is enough on hand; if not, the order is rejected. If there is,
you subtract the purchased quantity from stock. Forgetting this check means
selling what you don't have.
In the following exercises you'll build: the query for each order's total (SQL), the cart logic (JS), the purchase handler that validates stock (JS) and the Cart component (React).