envineer
Meta Breaker
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
400 XP
In this tutorial you will learn:
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.
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.
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.
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.
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.
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.
Book traversal links for NumPy Rounding Off
Download
- 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.
- #importing the numpy lib
- import
numpy as
np
- #applying the floor function on 1.88 and 1.11 separately
- res1 =
np.floor
(
1.88
)
- res2 =
np.floor
(
1.11
)
- print
(
'Result of Rounding off 1.88 using floor() function: '
,
res1)
- 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.
- #importing the numpy lib
- import
numpy as
np
- #applying the floor function on 2.99 and 2.01 separately
- res1 =
np.ceil
(
2.99
)
- res2 =
np.ceil
(
2.01
)
- print
(
'Result of Rounding off 2.99 using ceil() function: '
,
res1)
- 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.
- #importing the numpy lib
- import
numpy as
np
- #applying the fix() function on 4.15 and 4.92 separately
- res1 =
np.fix
(
4.15
)
- res2 =
np.fix
(
4.92
)
- print
(
'Result of Rounding off 4.15 using fix() function: '
,
res1)
- 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.
- #importing the numpy lib
- import
numpy as
np
- #applying the around function on 5.45 and 5.52 separately
- res1 =
np.around
(
5.45
)
- res2 =
np.around
(
5.52
)
- print
(
'Result of Rounding off 5.45 using around() function: '
,
res1)
- 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.
- #importing the numpy lib
- import
numpy as
np
- #applying the truncate function on 8.11 and 8.75 separately
- res1 =
np.trunc
(
8.11
)
- res2 =
np.trunc
(
8.75
)
- print
(
'Result of Rounding off 8.11 using trunc() function: '
,
res1)
- print
(
'Result of Rounding off 8.75 using trunc() function: '
,
res2)
Book traversal links for NumPy Rounding Off
- ‹ NumPy Random
- Up
- NumPy Selective Random ›
Download
You must upgrade your account or reply in the thread to view hidden text.