Start New Discussion within our Software Development Community

Math.round

Hi, could someone please add the math.code function in the code below so that the resulting answer in the div is to two decimal places.

<script type = <"text/javascript">

function calculate()

{

A = document.getElementById('input').value;

outDiv = document.getElementById('output');

B =(parseInt(A) * 1.0);

//alert(A);

D = (parseInt(B) * 1.0 );

if ( B <= 19)

{

tax = 0.00;

}

else if (B <= 30)

{

tax = (B * 3.95) * 1;

}

else if (B <= 40)

{

tax = (B * 3.75) * 1;

}

else if (B <= 50)

{

tax = (B * 3.70) * 1;

}

else if (B <= 60)

{

tax = (B * 3.55) * 1;

}

else if (B <= 70)

{

tax = (B * 3.40) * 1;

}

else if (B <= 80)

{

tax = (B * 3.25) * 1;

}

else if (B <= 90)

{

tax = (B * 3.10) * 1;

}

else if (B <= 100)

{

tax = (B * 2.95) * 1;

}

else if (B <= 150)

{

tax = (B * 2.80) * 1;

}

else if (B <= 200)

{

tax = (B * 2.65) * 1;

}

else

{

tax = (B * 2.60) * 1;

}

outDiv.innerHTML = '£'+tax;

}
</script>

</head>

<body>

<p style="margin-left: 3">
<b><font face="Arial" color="#008080" style="font-size: 9pt">Enter Quantity&nbsp;
</font><font face="Arial" color="#008080" size="2">&nbsp;</font></b> 
<INPUT TYPE="TEXT" id='input' NAME="text2" size="6" /> </p>
<p>&nbsp;<INPUT TYPE="button" VALUE="SUBMIT" onClick="calculate()" style="color: #800000; font-family: Arial;    font-size: 10px"/></p>
<p>&nbsp;</p>
<div id='output'> </div><br>


</body>
</html>

Matt
Two things you may have misunderstood...
1. This is not a "we do your homework" servce. Somebody may help you learn how to round your results, but nobody will just do it for you.
2. This is the Java forum. Java and JavaScript are not the same thing at all.

So: Please post a request for assistance in learning how to round values, and post it in the Web Development / JavaScript forum

Good luck
J

James, do you know that the link of JavaScript forum (or anything that are in the same web-development category) is broken? I am not sure if it is from the server URL-rewriting or something else... That may be the reason why this person posted here.

Anyway, to answer to the question (even though the thread is closed), you should parse the number, multiply by 100, compute the result, round it, then divide it again by 100.

var a = 234.244
var b = 51.654

var upA = a*100
var upB = b*100

var resA = Math.round((upA*3.35)) / 100  // <= 784.72
var resB = Math.round((upB*3.35)) / 100  // <= 173.04

James, do you know that the link of JavaScript forum (or anything that are in the same web-development category) is broken?

Do you mean the drop-down "Web Development" menu near the top of the page? It's working OK for me...

Not the drop down, you click on a (any) link on the drop down. :(

The link display is http://www.daniweb.com/web-development/javascript-dhtml-ajax/117 but after I clicked on it, it redirected to http://www.daniweb.com-development/javascript-dhtml-ajax/117.

Thanks James. Problem solved. Reinstalled Firefox because it seems that my FF broke for some reason.

This question has already been answered. Start a new discussion instead.