Start learning free

LESSON 24 OF 348 · CALCULATIONS WITH NUMBERS

Floor Division in TypeScript: Math.floor and the Remainder

Lesson Read the full explanation

TL ; DR:

  • The Math.floor() function returns the largest integer less than or equal to a given number:

    console.log(Math.floor(3.9)); // Output: 3
    console.log(Math.floor(5.2)); // Output: 5
    console.log(Math.floor(7)); // Output: 7
    
  • We can use this function if we want to compute the integral part of some division (quotient):

    let a = 3;
    let b = 7;
    
    console.log(Math.floor(20 / a)); // Output: 6
    console.log(Math.floor(b / a)); // Output: 2
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

Floor division

Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6

It can also be used with variables:

a = 3
b = 7

print(20 // a) # Output: 6
print(b // a) # Output: 2

Modulo

The modulo operator (%) calculates the remainder of dividing two values:

print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2

# Can be used with variables:
a = 2
b = 4

print(b % a) # Output: 0
print(11 % b) # Output: 3

Quotient and remainder

In programming, we combine both these concepts to get the quotient and remainder of some divison:

# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3

print(quotient) # Output: 8
print(remainder) # Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Floor Division in TypeScript, explained

    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;
    
    1. Divide the money by the price: amount / price is 4.666666666666667. That is a decimal, but nobody can buy two thirds of a product.
    2. 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.
    3. 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) prints 4.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) / price floors the amount first and then divides, so the decimals come back.
    • Swapping the operands. price / amount is 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

    • / on number values keeps the decimals. For the quotient use Math.floor(a / b).
    • a % b is the remainder, and the quotient times b plus the remainder gives back a.
    • With negative numbers, Math.floor and Math.trunc give different answers.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free