Skip to content Skip to sidebar Skip to footer

How To Round To Nearest Decimal In Python

This is my first time working with Python. I'm trying to figure out how to round decimals in the simplest way possible. print('\nTip Calculator') costMeal = float(input('Cost of M

Solution 1:

You should use Python's built-in round function.

Syntax of round():

round(number, number of digits)

Parameters of round():

..1) number - number to be rounded
..2) number of digits (Optional) - number of digits 
     up to which the given number isto be rounded.
     Ifnot provided, will round tointeger.

Therefore, you should try code more like:

print("\nTip Calculator")

costMeal = float(input("Cost of Meal: "))

tipPrct = .20print("Tip Percent: 20%")

tip = costMeal * tipPrct
tip = round(tip, 2) ## new lineprint("Tip Amount: " + str(tip))

total = costMeal + tip
print("Total Amount: " + str(total))

Post a Comment for "How To Round To Nearest Decimal In Python"