-6

I have 12.5, and I want to convert it to 13. How can I do this in Python 3?

The task was like this - "Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost"

I solved the problem in Python 3 and in 3 test cases, it shows that my code is working. but in 1 case it is not.

Where,

Sample input:

12.00

20

8

Expected output:

13

And my output was 12.5

How on earth I can take 12.5 as 13?

mealcost = float(input()) 
tippercent = float(input()) 
taxpercent = float(input())  

tippercent = mealcost * (tippercent / 100)  
taxpercent = mealcost * (taxpercent / 100) 

totalcost = float( mealcost + tippercent + taxpercent)  
print(totalcost)
berkelem
  • 1,573
  • 2
  • 13
  • 27

2 Answers2

1

Use round()

print(round(12.5))
>>> 13.0
berkelem
  • 1,573
  • 2
  • 13
  • 27
-2

To round to the nearest X (ie nearest 20.0)

  1. just divide by the value you want to round to
  2. then round the result
  3. then multiply it by the number you want to round to and cast to an int

for example

round_to_nearest = 20
for a_num in [9,15,22,32,35,66,98]:
    rounded = int(round(a_num/round_to_nearest)*round_to_nearest)
    print("{a_num} rounded = ".format(a_num=a_num,r=rounded))

to just round

oh nevermind it looks like you just want

print(round(12.3),round(12.6)) # 12, 13

if round rounds wrongly (ie round(12.5) => 12 in python3) you can just add 0.5 to your number and floor it

int(12.5+0.5)
Joran Beasley
  • 93,863
  • 11
  • 131
  • 160