/    Sign up×
Community /Pin to ProfileBookmark

need help with rounding

I am not a javascript coder so i apologize in advance but I have a formula that calculates off of 2 different inputs. My only problem is I want the result to be rounding to the nearest 100th. For example, the result may be something like 25.6789 and I would like it to calculate to 25.68 to only 2 decimal points and round it up to the nearest 100th.

Here is my current code:
<html>

<head>
<meta http-equiv=”Content-Language” content=”en-us”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<meta name=”GENERATOR” content=”Microsoft FrontPage 6.0″>
<meta name=”ProgId” content=”FrontPage.Editor.Document”>
<title>TOC Calculator</title>
<script LANGUAGE=”JavaScript”>

<!– Begin
function calcLeaguePoints() {
var participantCount = document.leaguePoints.participantCount.value * 1;
var finish = document.leaguePoints.finish.value *
1;

answer =Math.sqrt(participantCount * 30) / (finish + 1)*10;
document.leaguePoints.answer.value = answer;
return true;

}
// End –>
</script>

<meta name=”Microsoft Theme” content=”tidepool 000″>
</head>

<body background=”file:///C:/Program%20Files/Microsoft%20FrontPage/themes/tidepool/”
bgcolor=”#CC9966″ text=”#000033″ link=”#660000″ vlink=”#CC0000″ alink=”#FF3300″>

<form name=”leaguePoints”>
<p>&nbsp;</p>
<div align=”center”><center><p>Use the calculator below to figure out your TOC points.
&nbsp; </p>
</center></div><div align=”center”><center><p><strong>Participants</strong> is the total
number of people in the Tourney.</p>
</center></div><div align=”center”><center><p><strong>Your finish</strong> is what
position you placed.</p>
</center></div><p>&nbsp;</p>
<div align=”center”><center><table>
<tr>
<td bgcolor=”#FFFFCC”><div align=”center”><center><p>Points
Calculator</p>
</center></div><table border=”0″>
<tr>
<td align=”right” bgcolor=”#FFFFCC”><font
face=”Verdana” size=”2″><b>Participants:</b></font></td>
<td bgcolor=”#FFFFCC”><font face=”Verdana”
size=”2″><input type=”text” name=”participantCount” SIZE=”5″ maxlength=”2″> </font></td>
</tr>
<tr>
<td align=”right” bgcolor=”#FFFFCC”><b><font
face=”Verdana” size=”2″>Your finish:</font></b></td>
<td bgcolor=”#FFFFCC”><font face=”Verdana”
size=”2″><input type=”text” name=”finish” size=”5″ maxlength=”2″> </font></td>
</tr>
<tr>
<td align=”center” colspan=”2″ bgcolor=”#FFFFCC”><font face=”Verdana” size=”2″><input type=”button”
value=”Calculate TOC Points” onclick=”calcLeaguePoints()”> </font></td>
</tr>
<tr>
<td bgcolor=”#FFFFCC”><div align=”right”><p><b><font
face=”Verdana” size=”2″>TOC points:</font></b> </td>
<td bgcolor=”#FFFFCC” align=”center”><font
face=”Verdana” size=”2″><input type=”text” name=”answer” size=”2″></font></td>
</tr>
</table>
</td>
</tr>
</table>
</center></div>
</form>

<p align=”center”>&nbsp;</p>

</body>
</html>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@animalj7Jan 02.2009 — toFixed doesn't round, it truncates.

You can try something like this...

[CODE]<script language="javascript" type="text/javascript">
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
document.roundform.numberfield.value = newnumber; // Output the result to the form field (change for your purposes)
}
</script>[/CODE]


found here.
Copy linkTweet thisAlerts:
@maweber49Jan 02.2009 — do this:

multiply your answer times 100, then use the math.round method to get rid of the numbers to the right of the decimal point, then divide the result by 100 and you've got 2 place results

tempans=math.round(answer*100)

answer=tempans/100

example

answer=1234.5678

tempans=math.round(1234.5678*100) //this will = 123457.

answer=123457./100//this will = 1234.57

?
Copy linkTweet thisAlerts:
@wbportJan 02.2009 — Does this fly?
[CODE]// Function to round to two decimal places.
function round(x) {
return Math.round(x*100)/100;
[/CODE]
Copy linkTweet thisAlerts:
@NedalsJan 03.2009 — toFixed doesn't round, it truncates.[/QUOTE]
No!. It rounds
Copy linkTweet thisAlerts:
@animalj7Jan 03.2009 — No!. It rounds[/QUOTE]

My mistake. Yes, It does.
×

Success!

Help @gomester spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...