[Tutor] Difference between rounding off and typecasting to int

Ratheesh kumar vrkratheesh at live.com
Sat Jan 2 09:33:11 EST 2016


Hii everyone, Today i was just solving a problem in hacker-rank. A simple one to calculate the tip and tax for the meal. The resultant answer should be rounded off. I first wrote the code as below:
m=float(input())
x=int(input())
t=int(input())
tip=(m*x)/100
tax=(m*t)/100
total=m+tip+tax
print("The final price of the meal is $",end='')
print(int(total),end='')
print('.')
It passed three test-cases but the last test continued to fail. Then i came to know that the problem was with rounding off. Then i edited my code to the following:
m=float(input())
x=int(input())
t=int(input())
tip=(m*x)/100
tax=(m*t)/100
total=m+tip+tax
print("The final price of the meal is $",end='')
print(round(total),end='')
print('.')Finally all test-cases passed. But I can't get to understand what round() did int() cant't do to the variable total. Can anyone pinpoint the change. Thank you.











 		 	   		  


More information about the Tutor mailing list