Round Function

 '''The round() function in Python is used to round a number to a specified number of decimal places.

round(number, ndigits)'''

number=float(input("Enter a number: "))

ndigits=int(input("Enter the number of decimal places to round to: "))

print(round(number, ndigits))

# The first variable is the number to be rounded, and the second variable is the number of decimal places to round to.


print(round(3.14159, 2))   # Output: 3.14

print(round(5.678, 1))     # Output: 5.7

print(round(4.5))          # Output: 4 (rounds to the nearest even number) 

print(round(5.5))          # Output: 6 (rounds to the nearest even number)


# If ndigits is not specified, the default value is 0, which means rounding to the nearest integer.

print(round(3.14159))      # Output: 3


# If ndigits is negative, it rounds to nearest 10s,100s, etc. as base 10 numbers.

print(round(5.678, -1))    # Output: 10.0

print(round(4.5, -1))      # Output: 0.0

print(round(5432.5, -3))   # Output: 5000.0

Comments

  1. add another few examples of your own.. try different variations, like what happens when you try to round a negative float number

    With Love.

    ReplyDelete

Post a Comment

Popular posts from this blog

Print Function

Arithmetic Operators

Sets in Python