DevPath · Learn to code ESPTEN

Project: online store

Money in cents and stock control

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.

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).

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← The plan: model a storeView the module →