C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: This is the number we want to round. For this example, we are rounding the number 1.23456.
Argument 2: This tells how many numbers past the decimal point to keep. The final number is rounded up if the next digit is 5 or more.
Python program that uses round
number = 1.23456
# Use round built-in.
# ... This rounds up or down depending on the last digit.
print(round(number))
print(round(number, 0))
print(round(number, 1))
print(round(number, 2))
print(round(number, 3))
Output
1 0 digits
1.0 0 digits
1.2 1 digit
1.23 2 digits
1.235 3 digits, last one rounded up to 5
Ceil: This will always round up. So the ceil of 1.1 is 2. An integer is returned.
Floor: This will round down to the nearest integer. The floor of 1.9 is 1. This is a mathematical function.
Python program that rounds up, down
import math
number = 1.1
# Use math.ceil to round up.
result = math.ceil(number)
print(result)
# The round method will round down because 1.1 is less than 1.5.
print(round(number))
Output
2
1
Error message, math.round:
Traceback (most recent call last):
File "C:\programs\file.py", line 6, in <module>
number = math.round(1.1)
AttributeError: 'module' object has no attribute 'round'