Concept
Money as integer paisa
Why every amount in the code is a whole number of paisa, and what goes wrong with decimal taka.
Open any order in the database and its total looks a hundred times too big:
Mina's two General tickets are stored as 240000. That is 240,000 paisa,
৳2,400. Every amount in the application is a whole number of paisa, from the
ticket price to the day's revenue, and only one module ever converts to taka.
This page shows what that rule prevents, and where the code enforces it.
Rule: money is integer paisa
Amounts are BIGINT paisa in the database and number in code, never a decimal. Conversion
between taka and paisa lives only in money.ts.
Decimals are not exact
Computers store a number like 0.1 in binary, where it has no exact form, the way one third has no exact decimal form. JavaScript, like most languages, rounds it to the nearest value it can store:
0.1 + 0.2; // 0.30000000000000004
19.99 * 100; // 1998.9999999999998The second line is the one that costs money. Turn ৳19.99 into paisa by cutting off the fraction and you get 1,998, a paisa short. Each error is tiny; they add up across thousands of orders, and a total that should match a bKash statement no longer does.
Count the same order two ways
Pick a price, a quantity and the 15% DHAKA15 code. The left column does
plain JavaScript arithmetic on taka; the right one follows the application's
rules.
Illustration · same order, two ways to count
| Line | Floating taka | Integer paisa |
|---|---|---|
| Subtotal | 59.97 | 5,997 ৳59.97 |
| Discount | 8.995499999999998not a whole paisa | 897 ৳8.97 |
| Total | 50.9745not a whole paisa | 5,100 ৳51.00 |
| 1,000 such orders | 50974.49999999893not a whole paisa | 5,100,000 ৳51,000.00 |
The floating column is plain JavaScript arithmetic on taka. The paisa column follows the app's rules: one conversion to whole paisa, then whole-number arithmetic only, with a percentage discount rounded down on each ticket.
Try ৳19.99, three tickets, with the code. The floating total is 50.9745 taka: not a whole paisa, so something would have to decide which way to round it, and every place that rounds might decide differently. In paisa the answer is exact, 5,100 (৳51.00), because the discount is worked out per ticket and rounded down: 15% of 1,999 paisa is 299.85, so each ticket gets 299 off. Rounding down means a buyer never gets a paisa more than the organizer offered. Add up a thousand such orders and the floating sum drifts further from any real amount, while the paisa sum stays exact.
Where the code enforces it
Taka becomes paisa once, at the edge, rounded to the nearest paisa so
19.99 becomes 1,999:
export function takaToPaisa(taka: number): number {
if (!Number.isFinite(taka)) {
throw new RangeError(`taka must be a finite number, got ${taka}`);
}
if (taka < 0) {
throw new RangeError(`taka must be non-negative, got ${taka}`);
}
const paisa = Math.round(taka * PAISA_PER_TAKA);
assertValidPaisa(paisa);
return paisa;
}A percentage discount never touches a decimal. paisa × percent is a whole
number, and the one division is rounded down:
export function percentOfPaisa(paisa: number, percent: number): number {
assertValidPaisa(paisa);
if (!Number.isInteger(percent) || percent < 0 || percent > 100) {
throw new RangeError(`percent must be an integer from 0 to 100, got ${percent}`);
}
const scaled = paisa * percent;
assertValidPaisa(scaled);
return Math.floor(scaled / 100);
}The order's totals are computed in one place, from the ticket type's stored price, never from the request:
export function computeOrderTotals({
unitPricePaisa,
quantity,
discountPaisa = 0,
}: OrderTotalsInput): OrderTotals {
const subtotalPaisa = multiplyPaisa(unitPricePaisa, quantity);
assertValidPaisa(discountPaisa);
// A fixed-amount code larger than the order never produces a negative
// total or a credit — it is capped at what the order is worth.
const applied = Math.min(discountPaisa, subtotalPaisa);
const totalPaisa = sumPaisa(subtotalPaisa) - applied;
assertValidPaisa(totalPaisa);
return { unitPricePaisa, quantity, subtotalPaisa, discountPaisa: applied, totalPaisa };
}When something is wrong
Every helper checks its input with assertValidPaisa: a fraction, a negative
amount or one past Number.MAX_SAFE_INTEGER, where JavaScript stops counting
exactly, throws instead of producing a wrong total. See the money tests,
the pricing tests
and the promo tests.
The database checks the arithmetic again, whatever code wrote the row. A
CHECK constraint on orders requires the subtotal to equal the unit price
times the quantity, and the total to equal the subtotal minus the discount.
Those are exact equalities, which only whole numbers can promise. See the schema,
and the promo codes decision
for why discounts are per ticket and rounded down.
Read next
Source revision: 4da5b5f · View this page's source