How to round off in Python?

Did you ever get a half mark extra, like 38 and a half for 40 in exams? What did you tell your score was when asked? Did you tell it was 38 or 39? How did 38 and a half become either 38 or 39? That is the round figure, and this process is called rounding off.

There are recurring decimal numbers like 3.666666. While solving problems, we round it off and say 3.67 to make it simple and easy to solve. In the same way, we can round off floating-point numbers to the nearest precision numbers in python using a special predefined function – round ().

It might seem very simple. Yes, it is. But there are a lot of important points to keep in mind given the unusual behavior of the function. This tutorial will discuss everything we need to know about the round () function and other ways using which we can round off in Python.

Contents:

  • Syntax
  • Simple Example
  • Round half to even
  • Using the second parameter
  • Unusual behavior of the function
  • Type Error
  • Other ways of rounding off
  • Custom rounding up and down using Math Library
  • Using Numpy module
  • Application

Syntax:

round (number, digit count)

Here, number is the number that needs to be rounded off

digit count: This is an optional parameter. It specifies the total number of digits up to which the number should be rounded off. By default, the value will be 0.

  • If the second parameter is not given, the function rounds off the float into an integer.

Simple Example:

num = float (input ("Enter the floating point number to be round off: "))

res = round (num)

print ("The integer equivalent: ",res)

Output:

Enter the floating-point number to be round off: 3.65777

The integer equivalent:  4

Understanding:

If the last decimal digit until the rounding is done is less than 5, the previous number will be given. If the digit is greater than 5, the succeeding integer is given as output.

Examples:

print (round (9.008))

print (round (6.555))

print (round (8.978))

Output:

9

7

9

Round half to even:

What if you are supposed to round off 4.5, will it be 4 or 5? Or 7.5, will it be 7 or 8? 4.5 is equidistant from both 4 and 5 and so is 7.5 from 7 and 8. So, how does this work? We’ll check first:

print(round(4.5))

print(round(7.5))

Output:

4

8

This is called “Round half to even”. If the decimal is equidistant from both the options, consider the even option.

  1. The two options for 4.5 are 4 and 5-> even: 4
  2. The two options for 7.5 are 7 and 8-> even: 8

Using the second parameter:

The second parameter specifies the number of digits needed after or before the decimal point after rounding.

  1. +ve value -> after decimal point
  2. –ve value -> before decimal point

The number is taken up to the specified digit and

  1. If the next digit is given and is greater than 5 -> The specified digit + 1

Example: round(3.47, 1) -> 3.5

  • If the next digit is given and is less than 5 -> The specified digit

Example: round(3.43, 1) -> 3.4

  • If the next digit is given and is equal to 5 -> Even of the options

Example: round(3.45, 1) ->3.4

Example program:

print (round (9.008, 3))

print (round (6.555, 2))

print (round (8.978, 1))

Output:

9.008

6.56

9.0

Understanding:

  1. In the first statement, the number has 3 decimal digits – 9.008, and we want it rounded off until three digits-9.008. There is no digit after the third digit to consider moving to the next number. So, it stays as 9.008.
  2. The second case is 6.555, we want it rounded off till 2 digits. Look at the number till the second digit-6.55. Now, the next digit is 5, so it becomes 6.56 – increment by 0.01.
  3. The third statement is 8.978, and it needs to be rounded so that there is one digit after the decimal point. After the one digit-8.9, the next digit is 7. So, it gets rounded off to the next number-9.0

Negative second parameter:

A negative second parameter rounds off the digits on the left side of the decimal point-> the integer part:

0 -> 1’s position

1 -> 10’s position

2 -> 100’s position…

Examples:

print(round(1005.0, -1))

print(round(1550.0, -2))

print(round(1500.0, -3))

Output:

1000.0

1600.0

2000.0

1. -1 refers to the 10th position: 1005.0 -> 05.0 -> 00.0 or 10.0 even: 00.0 -> 1000.0

2. -2 refer to the 100th position: 1550.0 -> 550.0 -> 500.0 or 600.0 even: 600.0 -> 1600.0

3. -3 refer to the 1000th position: 1500.0 -> 1000.0 or 2000.0 even: 2000.0

Examples:

print(round(5.009, -4))

print(round(5.009, -3))

print(round(5.009, -2))

print(round(5.009, -1))

print(round(5.009, 0))

print(round(5.009, 1))

print(round(5.009, 2))

print(round(5.009, 3))

print(round(5.009, 4))

Output:

0.0

0.0

0.0

10.0

5.0

5.0

5.01

5.009

5.009

Negative first parameter:

Only the number changes, the whole mechanism remains the same.

print(round(-5.786, 0))

print(round(-5.786, 1))

print(round(-5.786, 2))

print(round(-5.786, 3))

Output:

-6.0

-5.8

-5.79

-5.786

Negative first and second parameters:

print(round(-5.786, -2))

print(round(-5.786, -1))

Output:

-0.0

-10.0

Unusual behavior of the function:

The behavior of the round () function is sometimes altered. Some decimal fractions cannot be represented correctly or exactly as a floating-point number.

In the case of some decimal numbers like 2.675, if we want to round it off to two decimal digits, we expect we get the result of 2.68, but instead, we get 2.67.

2.675 When converted to binary float, we get the approximation as 2.6799999999822364… hence; it further gets rounded into 2.67 rather than 2.68.

Program:

from decimal import Decimal

num = 2.675

print (round (num, 2))

num = Decimal ('2.675')

print (round (num, 2))

Output:

2.67

2.68

Understanding:

We imported the decimal module to use float arithmetic. In the first case, we used the normal way, for which we got 2.67. Now, in the second case, before rounding it, we used the decimal function and then rounded it off, giving us the result we needed- 2.68.

  1. If we give more digits for rounding than the total number of decimal digits in the number, we won't run into an error.
  2. We won't run into an error even if we give a negative number.

Here is an example:

print (round (9.008, 9))

print (round (9.008, -8))

Output:

9.008

0.0

Understanding:

In the first case, we gave more digits, so it leaves the number as it is. In the second case, we gave a negative number or way too lesser number of digits in another sense. So, it cancels off the whole number and gives 0.0

We can get one error though:

Type error:

This happens when we do not give integer or floating-point numbers in the round function and give anything other than numbers like characters, strings:

Example:

print (round (“gh”, 3))

Output:

print (round ("gh", 3))

TypeError: type str doesn't define __round__ method

We covered everything there is about the round() function. Now, let us see other ways using which we can round off in Python:

1. Custom rounding up and down using the Math library:

    We saw that if the digit is greater than 5, the number is rounded off to the succeeding number else, it stays the same. Using the math module, we can control this. Here is an example:

    import math

    num = 4.2

    print("Number:", num)

    print("Rounding down:", math.floor(num))

    print("Rounding up:", math.ceil(num))

    num1 = 4.8

    print("Number:", num1)

    print("Rounding down:", math.floor(num1))

    print("Rounding up:", math.ceil(num1))

    Output:

    Number: 4.2

    Rounding down: 4

    Rounding up: 5

    Number: 4.8

    Rounding down: 4

    Rounding up: 5

    math.floor() -> Rounds down

    math.ceil() -> Rounds up

    2. Using Numpy module:

    import numpy as np

    array = np.array([3.4, 3.9, 4.2])

    rounded = np.round(array)

    print(rounded)

    Output:

    [3. 4. 4.]

    We can also give the position up to which we want the number to e rounded off:

    np.round(array, decimals = 4)

    Application:

    Recurring fractions

    Suppose we have a fraction 2/3:

    print(2/3)

    Output:

    0.66666666666666

    If we want to perform different operations on recurring fractions like these, we need not consider the whole decimal part, we can just round them off to two digits after the decimal point.