Can someone tell me how to use Math .ceil in this html. I cant seem to get it to round the tip amount.( for a school assignment)


#1

<!doctype html>
<!-- tip.html                             josh weaver -->
<!-- this web page calculates the tip amount on bill. -->
<!-- ================================================ -->

<html>
<head>
<title> Tip Calculator </title>
</head>

<body>
<h2>tip calculater</h2>
<p>
Enter Check amount : $<input type="text" id="amountBox" size=10 value="">
<br>
Tip percentage : 15%
</p>
<input type="button" value="Calculate tip"
onclick="amount=parseFloat(document.getElementById('amountBox').value);
tip = amount * (15/100);
document.getElementById('outputDiv').innerHTML= 
'you should tip $' + tip;">
<hr>
<div id="outputDiv"></div>
</body>
</html>

#2

That depends, what are you trying to achieve? If you want it to show in the output, you can simple call it in the output:

document.getElementById('outputDiv').innerHTML= 
'you should tip $' + Math.ceil(tip);">

But of course, this depends on what you want to achieve


#3

Thank you . attempting to get it to give me the tip amount rounded to the second decimal place..


#4

Why then, not simply use math.round?


#5

It only wants it to round up , vs the math.round which goes either way


#6

well, next time, could you in one time specify what you are trying to achieve? I haven't tested many values, but this seems to do fine:

tip = Math.ceil(amount * 15) / 100;

only problem, if people enter: 0.00001 this will still say you need to tip 0.01


#7

SURE Thanks for you help