Python round() Built in Function
The round() function in Python returns a floating-point number rounded to the specified number of decimals. The syntax is:
round(number, ndigits)
where number is the number to be rounded and ndigits is the number of decimal places to round to. If ndigits is omitted or None, then the number is rounded to the nearest integer.
Here are some examples of using round() with different values of ndigits:
# Round 13.46 to the nearest integer
print(round(13.46)) # Output: 13
# Round 13.46 to one decimal place
print(round(13.46, 1)) # Output: 13.5
# Round 1.5 up and 2.5 down
print(round(1.5)) # Output: 2
print(round(2.5)) # Output: 2
# Round -1.25 to zero decimal places
print(round(-1.25)) # Output: -1
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment