sec01 - round()

round()

The “round()” function rounds the number specified in parentheses to the nearest integer.

(When rounding the value “.5,” the result may differ from what you expect. Please check whether you can accept this difference before using this function.)

Now, let's run the code and check how it behaves.

# 1.0, 1.49, 1.5, 1.51
result_1_00 = round(1.0)
result_1_49 = round(1.49)
result_1_50 = round(1.5)
result_1_51 = round(1.51)
print('round(1.00):', result_1_00)
print('round(1.49):', result_1_49)
print('round(1.50):', result_1_50)
print('round(1.51):', result_1_51)

The result should be as shown in the figure below. This should be rounded in the same way as you would expect. “round(1.5)” returns “2.”

Now, let's try executing the following code.

# 2.0, 2.49, 2.5, 2.51
result_2_00 = round(2.0)
result_2_49 = round(2.49)
result_2_50 = round(2.5)
result_2_51 = round(2.51)
print('round(2.00):', result_2_00)
print('round(2.49):', result_2_49)
print('round(2.50):', result_2_50)
print('round(2.51):', result_2_51)

The result should look like the figure below. The result of “round(2.5)” is “2” instead of “3,” which may differ from your expectations. We will explain why this happens in the next section.

round to even

The Python round function uses ‘round to even’ rounding. Rounding to even has the following characteristics.

  • If the fraction is less than 0.5, “rounded down” is applied.
  • If the fraction is greater than 0.5, “rounding up” is applied.
  • If the fraction is exactly 0.5, rounding is applied to the result that is an even number.

In other words, when the number is exactly “.5,” the result rounded to the even number is returned, which is different from what you have learned so far and what you would expect. This is shown in the following figure.

If the number is slightly larger than “.5,” such as “.500001,” it will be “rounded up,” and if it is slightly smaller, such as “.4999999,” it will be “rounded down.”

This method has the advantage of being less biased than normal rounding. For this reason, other programming languages besides Python use this type of rounding behavior. However, not all languages adopt this method, so be sure to check the specifications of each language.

※ In Python 2, “.5” is rounded up. This behavior differs from Python 3; please be cautious.

Rounding other than to the first decimal place

When rounding the first decimal place to an integer, rounding to even was performed in cases where the value was exactly “.5.” In other words, “when the value corresponding to the digit to be rounded is ‘5’ and is followed by zeros, rounding to even is performed.”

This is also true when rounding off the second decimal place or higher, or when rounding off the tens, hundreds, etc. of the integer part.

For example, when rounding the 100s place of “500.0,” the 100s place is “5,” and since it is followed by zeros (“00.0”), it is rounded to an even number, resulting in “0.0.” If the number is slightly larger than “500.0,” such as “500.0001,” it is rounded up to “1000.0.”

Specifying digits

The “round()” function allows you to specify the number of digits to be rounded.

Separate the contents of the parentheses in “round()” with commas (,) and specify the number you want to round and the number of digits to which you want to round it. Write the code as follows.

result0 = round(123.456, 0)
result1 = round(123.456, 1)
result2 = round(123.456, 2)
print('ndigits 0:', result0)
print('ndigits 1:', result1)
print('ndigits 2:', result2)

The above code rounds “123.456” to the specified number of digits.

The first line is “round(123.456, 0),” with the digit specification set to “0.” In this case, the first decimal place is rounded to “123.0.” When the digit specification is “0,” the behavior is the same as when no digit specification is specified (“round(123.456)”).

Regarding the second and third lines, each time you increment the digit specification value by one, you can specify the number of digits after the decimal point in order, such as “1” for the second decimal place and “2” for the third decimal place. When the above code is executed, the results returned are “123.5” (rounded to the second decimal place) and “123.46” (rounded to the third decimal place).

There is no upper limit on the number of digits specified, so you can specify “3” or more digits.

When a value greater than the number of decimal places in the original value is specified.

If you specify a value greater than the number of decimal places in the original value, the original value is returned as is.

For example, “round(123.456, 3)” is equivalent to attempting to round the fourth decimal place of “123.4560,” and since rounding the fourth decimal place0” would result in truncation, the result is “123.456.”

Next, let's try specifying the digit with a negative value.

result_1 = round(123.456, -1)
result_2 = round(123.456, -2)
print('ndigits -1:', result_1)
print('ndigits -2:', result_2)

When you run this code, “120.0” and “100.0” are returned if you specify the number of digits with a negative value, “-1” rounds the integer part to the 1s place, “-2” rounds to the 10s place, and so on, with the number of digits in the integer part increasing in order.

There is no upper limit on the number of digits for the integer part.

When the number of digits specified exceeds the number of digits in the integer part

If the specified number of digits exceeds the number of digits in the integer part of the original value, “0.0” is returned.

For example, in the case of “round(123.456, -4),” this is equivalent to attempting to round the 1000s place of “0123.456,” and since rounding the “0” in the 1000s place results in truncation, the result is “0.0.”

おすすめの記事