We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,956 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

math.round

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>
3
Contributors
6
Replies
1 Day
Discussion Span
4 Months Ago
Last Updated
7
Views
Question
Answered
mattsearle
Newbie Poster
11 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30
Question Answered as of 5 Months Ago by JamesCherrill

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
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1436 seconds using 2.87MB