xxxxxxxxxx
print(round(2.555, 2))
print(round(2.786, 2))
print(round(2.553, 2))
xxxxxxxxxx
int x = 6.3456824221
#round(number_to_roundoff, round_off_till)
#round_off_till is optional
print(round(x)) #output: 6
print(round(x, 3)) #output: 6.346
print(round(x, 1) #output: 6.3
xxxxxxxxxx
#round(number, decimal_place=0)
print(round(4.355679)) #Prints 4
print(round(4.355679, 3) #Prints 4.356
xxxxxxxxxx
import math
PI = math.pi
print(PI)
#output
#3.141592653589793
round(PI)
#output
#3
xxxxxxxxxx
import math
n = 234.56
decimal_place = n - int(n)
''' decimal_place = 234.56 - 234 = 0.56 '''
if decimal_place >= 0.5:
print(math.ceil(n))
else:
print(math.floor(n))
''' Yields 234.56 --> 235 '''
''' One-Liner '''
rounded = math.ceil(n) if (n-int(n)) >= 0.5 else math.floor(n)
xxxxxxxxxx
#round print
process = 1345 * 0.75
print(f"The result can be shown as {round(process, 1)}") #output:1008.8
print(f"The result can be shown as {round(process, 2)}") #output:1008.75
xxxxxxxxxx
# Rounding a number in Python
# Using round() function with default precision
num1 = 3.14159
rounded_num1 = round(num1)
print(rounded_num1) # Output: 3
# Using round() function with custom precision
num2 = 5.6789
rounded_num2 = round(num2, 2) # Round to 2 decimal places
print(rounded_num2) # Output: 5.68
# For rounding to a specific decimal place without using round()
num3 = 7.54321
rounded_num3 = int(num3 * 10) / 10 # Round to 1 decimal place (by multiplying and converting to int)
print(rounded_num3) # Output: 7.5
xxxxxxxxxx
# round(number, decimals).
# second parameter is optional, defaults to 0.
num1 = 1.56234
num2 = 1.434
print(round(num1)) # output: 2
print(round(num2)) # output: 1
print(round(num1, 3)) # output: 1.562
print(round(num2, 1) # output: 6.3