For example, let's take the number 4.5 and round it using the bank method. The closest even numbers to it are 4 and 6. This means that the result of rounding will be 4, since the number 4 is closer to the original number than 6. But the number 5.5 will be rounded to 6, since this time the number 6 is closer to the original number than 4.
Here is a small example source code that illustrates the use of the round() function in Python:
num = round ( 3.5 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
4
The example above is without the second argument. Let's take another number and add a second argument:
num = round ( 6.45892 , 2 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
6.46
As you can see, using the round() function is not difficult at all. Let's look at the following function.
Read also
Igor Dolgov: “After a month of Python intensive, they called me and said: “If you feel you can handle it, join us”
Function int()
This is also a built-in function, which does not require ecuador telegram data any imports to work. Using this function is a rather crude way of rounding, which works against all the rules of arithmetic. In essence, this function simply discards all the digits after the decimal point, in other words, it removes the entire fractional part of the number.
Here is a simple example:
num = int ( 6.452 )
print ( num )
As a result we get:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
6
num = int ( 6.9 )
print ( num )
Result:
alex@alex-pc:~$ /bin/python3 /home/alex/test.py
6
This method is ideal if you only need the integer part of a number. Next, let's look at the functions from the math module.
We get the same result if we take the number 6.9:
-
- Posts: 720
- Joined: Fri Dec 27, 2024 12:36 pm