Quotient and remainder
When you divide one whole number by another, the result has two parts. The quotient is how many times the divisor fits completely into the number. The remainder is what is left over.
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
Many problems need the quotient: how many full boxes can be filled, how many items can be bought, how many complete groups exist.
There is no integer division operator
In TypeScript, / always gives the exact result as a floating-point number. It never drops the fractional part, because all numbers share one type, number:
console.log(15 / 4); // 3.75
console.log(20 / 3); // 6.666666666666667
Some other languages have an operator for integer division, but in TypeScript a double slash // starts a comment, so it can't be used for this. To get the quotient, you divide and then round down with Math.floor.
Math.floor and the remainder operator
Math.floor(x) returns the largest integer that is less than or equal to x.
console.log(Math.floor(3.9)); // 3
console.log(Math.floor(5.2)); // 5
console.log(Math.floor(7)); // 7
Combining it with division gives the quotient:
let a: number = 3;
let b: number = 7;
console.log(Math.floor(20 / a)); // 6
console.log(Math.floor(b / a)); // 2
The remainder comes from the % operator:
console.log(15 % 4); // 3
console.log(20 % 3); // 2
const quotient: number = Math.floor(26 / 3);
const remainder: number = 26 % 3;
console.log(quotient, remainder); // 8 2
The two always fit together: the divisor times the quotient, plus the remainder, gives back the original number. Here 3 times 8 plus 2 is 26.
Negative numbers
For positive numbers, rounding down and dropping the fraction are the same thing. For negative numbers they differ. Math.floor rounds toward negative infinity and Math.trunc rounds toward zero:
console.log(Math.floor(-7 / 2)); // -4
console.log(Math.trunc(-7 / 2)); // -3
console.log(-7 % 2); // -1
Notice that % takes the sign of the left operand, so -7 % 2 is -1, not 1. It pairs with Math.trunc: the quotient -3 times 2, plus the remainder -1, gives -7. If you pair % with Math.floor on negative numbers, the two no longer fit together. For the exercise, all the numbers are positive, so Math.floor is right.
The exercise
A product costs price dollars and Andy has amount dollars. He can't buy a fraction of a product, so the answer is the number of whole products he can afford. The starter code is:
let amount: number = 70;
let price: number = 15;
- Divide the money by the price:
amount / priceis 4.666666666666667. That is a decimal, but nobody can buy two thirds of a product. - Round down with
Math.floor, giving 4. Rounding to the nearest integer would give 5, which Andy can't pay for, so rounding down is the right choice. - Print it with
console.log.
let amount: number = 70;
let price: number = 15;
console.log(Math.floor(amount / price));
It prints 4. Andy spends 60 dollars and keeps 10, which is amount % price.
Another use: splitting seconds
The quotient and remainder together split a total into units. To turn 125 seconds into minutes and seconds, the quotient by 60 is the minutes and the remainder is the seconds left over:
const total: number = 125;
console.log(Math.floor(total / 60), total % 60); // 2 5
That is 2 minutes and 5 seconds. The same pattern works for any pair of units, such as days and hours, or boxes and loose items.
Common mistakes
- Forgetting
Math.floor.console.log(amount / price)prints4.666666666666667. - Using
Math.round. It gives 5 here. Use it only when the nearest whole number is what you need. - Flooring the wrong thing.
Math.floor(amount) / pricefloors the amount first and then divides, so the decimals come back. - Swapping the operands.
price / amountis 0.21428571428571427, and flooring it gives 0. - Writing
//. TypeScript reads everything after it as a comment.
Before you submit, try the formula with numbers that divide evenly, such as 60 and 15. Then the quotient is exactly 4 and the remainder is 0, which shows that Math.floor leaves exact results unchanged and only affects results with a fractional part.
Key takeaways
/onnumbervalues keeps the decimals. For the quotient useMath.floor(a / b).a % bis the remainder, and the quotient timesbplus the remainder gives backa.- With negative numbers,
Math.floorandMath.truncgive different answers.