Skip to main content

NumPy Rounding Off


In this tutorial you will learn:

  • What is rounding off the decimal?
  • Python functions that could round off the decimals?
  • Python Syntax

Rounding off the decimals

Rounding off is a simple technique for conversion of float numbers to integers, however, the resultant value will be less accurate (as we remove the part after decimal), but easier to use. The data in type of most of the arrays as a programmer we will be encountering while coding in python will be the integers and floats. Number of times programmers have to round off the float numbers and convert them to integers. It is a very common process, either the float number can be rounded off to the floor value or the ceil value, it is totally dependent on the requirement of the programmer

Python Functions for Rounding Off

Python provides the following functions for rounding off the float numbers.

  • Floor
  • Ceil
  • Fix
  • Around
  • Trunc

We will be demonstrating the results of each function using the examples for each.
In this example we will be rounding off the float number to the lower decimal using the float function, here we can observe that no matter the value after the decimal, the float number will be rounded off to the nearest lower integer.

  1. #importing the numpy lib
  2. import numpy as np
  3. #applying the floor function on 1.88 and 1.11 separately
  4. res1 = np.floor(1.88)
  5. res2 = np.floor(1.11)
  6. print('Result of Rounding off 1.88 using floor() function: ', res1)
  7. print('Result of Rounding off 1.11 using floor() function: ', res2)

In this example we will be rounding off the float numbers using ceil() function, in the results you can observe that whatever value we write after the decimal point, the number will be rounded to the nearest greater integer.

  1. #importing the numpy lib
  2. import numpy as np
  3. #applying the floor function on 2.99 and 2.01 separately
  4. res1 = np.ceil(2.99)
  5. res2 = np.ceil(2.01)
  6. print('Result of Rounding off 2.99 using ceil() function: ', res1)
  7. print('Result of Rounding off 2.01 using ceil() function: ', res2)

In this example we will use the fix() function to round off the integer. In the results we can observe that the part of float number after the decimal has been removed.
  1. #importing the numpy lib
  2. import numpy as np
  3. #applying the fix() function on 4.15 and 4.92 separately
  4. res1 = np.fix(4.15)
  5. res2 = np.fix(4.92)
  6. print('Result of Rounding off 4.15 using fix() function: ', res1)
  7. print('Result of Rounding off 4.92 using fix() function: ', res2)

In this example we will be using the around() function for rounding off the numbers, in the results you can observe that the number is rounded off logically, that if the decimal part of the number is lesser or equal to 5, the number will be rounded off to the nearest lower integer otherwise it will be rounded off to the nearest greater integer.
  1. #importing the numpy lib
  2. import numpy as np
  3. #applying the around function on 5.45 and 5.52 separately
  4. res1 = np.around(5.45)
  5. res2 = np.around(5.52)
  6. print('Result of Rounding off 5.45 using around() function: ', res1)
  7. print('Result of Rounding off 5.52 using around() function: ', res2)

In this example, we will use the trunc() function to round off, the results of the fix() and trunc() function are the same, as they just truncate the decimal part of the number.
  1. #importing the numpy lib
  2. import numpy as np
  3. #applying the truncate function on 8.11 and 8.75 separately
  4. res1 = np.trunc(8.11)
  5. res2 = np.trunc(8.75)
  6. print('Result of Rounding off 8.11 using trunc() function: ', res1)
  7. print('Result of Rounding off 8.75 using trunc() function: ', res2)

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.