Operators for Getting Whole Numbers
-
Introduction
In this article, we will explain how to use some more advanced operators to work with numbers.
Rounding a Number
A number can be a “whole number” or a “decimal number”. Whole numbers are what we use to count things, such as 0, 1, 10, etc. Decimal numbers are those coming in-between whole numbers, such as 0.5, 1.1, 3.1415, etc.
Sometimes we need to find the nearest whole number for a decimal number. For example, suppose a player takes 12.35 seconds to complete a game. We can “round it” to 12 seconds to keep it simple. For another example, suppose a dog is 3.75 years old, we can “round it” to 4 years old when we describe the dog.
To round a number, we use the “round” operator, which is a reporter block:
We can test how it works by clicking on it:
If a number is negative, we can round it to the nearest negative whole number as well:
Ceiling of a Number
The “ceiling” operator is similar to the “round” operator. It also converts a decimal number to a whole number.
However, it will always give us a whole number that is equal to or larger than the number we give it.
The ceiling operator can be useful when we don’t want the whole number to be smaller than our input. For example, suppose you are calculating the shoe size of a person. Suppose you have measured that the length of the foot is 8.4 inches, then you can use the ceiling operator: the ceiling of 8.4 is 9. This is better than using the “round” operator, since the “round” operator would tell us the shoe size is 8, which would be too small for this person.
Floor of a Number
The “floor” operator is similar to the “ceiling” operator. It also converts a decimal number to a whole number.
However, it will always give us a whole number that is equal to or smaller than the number we give it.
The floor operator can be useful when we don’t want the whole number to be larger than our input. For example, suppose you have 2.7 dollars, and you want to calculate how many burgers you can buy. Each burger is 1 dollar, so you can take the floor of 2.7 to get 2. That is, you can spend 2 dollars to get 2 burgers.
If you use the “round” or “ceiling” operators, you would get 3 burgers, which is wrong. You only have 2.7 dollars, so you need to make sure the whole number you get is not larger than 2.7.