1,105,594 Community Members

math.round

Member Avatar
mattsearle
Newbie Poster
11 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>
Member Avatar
JamesCherrill
... trying to help
10,399 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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

Question Answered as of 1 Year Ago by JamesCherrill
Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

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
Member Avatar
JamesCherrill
... trying to help
10,399 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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...

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,399 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

No, I don't get that redirect. The link takes me to http://www.daniweb.com/web-development/javascript-dhtml-ajax/117 exactly as expected.
Have you tried clearing your browser cache? Using different browsers?

If this persists, please PM it to one the site Admins - deceptikon, happygeek, or ~s.o.s~

thanks
James

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article