Rounding functions from the math module
Before using functions from the math module, this module usa telegram data must first be imported. To do this, at the very beginning of the source code, write:
import math
And now you can safely use any functions from this module. Next, we will look at three functions that are used to round numbers.
Function math.ceil()
This function rounds up. Here is an example:
num = math.ceil ( 6.8 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
7
Function math.floor()
And this function rounds down. Example:
num = math. floor ( 6.8 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
6
This is an analogue of the already discussed int() function. It does the same thing: it discards the fractional part, leaving only the whole number. Example:
num = math. trunc ( 17.231449 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
17
Now let's talk about using another module to round numbers.
The quantize() method from the decimal module
Before using this module, you first need to import it: