/    Sign up×
Community /Pin to ProfileBookmark

How to round variables up to 2 decimal places?

Hi

My javascripting is getting there, I have been learning for the last week and I have produced a working script however im getting stuck with how you round a variable (stored user input) up to the nearest whole number to 2 decimals

I have looked at many tutorials and I think that the math.round has to be used somewhere but I cannot work out the syntax to integrate it into my code because everywhere I have looked seems to be using document.write syntax:

[CODE]
<html>
<head>
<script language=”javascript”>
function measurements()
{
var val1 = parseInt(document.getElementById(“value1”).value);
var val2 = parseInt(document.getElementById(“value2”).value);
var val3 = parseInt(document.getElementById(“value3”).value);
var ans1 =document.getElementById(“answer”)
var ans2 =document.getElementById(“answer2”)
var ans3 =document.getElementById(“answer3″)
ans1.value = (val1*val2) /10000;
ans2.value = (val1*val2) /10000 *2;
ans3.value = (val1*val2) /10000 *2 * val3 *1.05;
}
</script>
</head>
<body>
<h1>Calculating plain roof tiles</h1>
<p>Please use our online calculator to calculate an estimate of how many plain roof tiles you need for the whole of a gable to gable roof. The calculator makes three measurements to calculate the amount needed:</p>
<h3><strong>Plain roof tile calculator</strong></h3>
<p> </p>
Enter length of the roof (eaves length): <input type=”text” id=”value1″ name=”value1″ size = 7 value””/>cm
<P>
Enter length of the gable end roof verge: <input type=”text” id=”value2″ name=”value2″ size = 5 value””/>cm
<P>
How many plain roof tiles per square metre: <input type=”text” id=”value3″ name=”value3″ size = 1 value””/>
<P>
<input type=”button” name=”Submit” value=”Calculate tiles needed” onclick=”javascript:measurements()”/>
<P>
Half of the roof area is <input type=”text” id=”answer” name=”answer” size = 1 value”/> m2. The whole roof area is <input type=”text” id=”answer2″ name=”answer2″ size = 1 value”/> m2
<P>
<h4><strong>The whole roof:</strong></h4>
<B>
<P>
Approximately <input type=”text” id=”answer3″ name=”answer3″ size = 2 value”/> plain roof tiles are needed for this type of roof (including 5% wastage)</B>
<P>
Please note that this is an approximate estimation based on a mathematical formula. If you need a more accurate figure we recommend that you carry out your own independent calculation
<P>
</body>
</html>
[/CODE]

Could anyone help megive me pointers with how I can achieve rounding up to two decimal places for ans1,2 and 3?

Thanks in advance, this is a great forum and have learnt so much already

Jamie

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJan 11.2010 — ans1.value = (val1*val2/10000).toFixed(2);

If you want to round it up and have '.00' tacked on the end, use

ans1.value = Math.ceil(val1*
val2/10000).toFixed(2);
Copy linkTweet thisAlerts:
@mityaJan 11.2010 — Harder than you'd imagine. One of JS's weakness is its lack of inbuilt decimal place rounding function. Here's how you do it:

[CODE]var myNum = 1.32531;
var myNumToTwoDecimalPlaces = Math.round(myNum*Math.pow(10,2))/Math.pow(10,2);
alert(myNumToTwoDecimalPlaces);[/CODE]


(2 refers to the decimal places desired)
Copy linkTweet thisAlerts:
@Declan1991Jan 12.2010 — toFixed and toPrecision are the built in methods, to decimal places and significant figures respectively. The disadvantage with them is they don't remove trailing zeros, but you can either use the above solution, or a regular expression to remove them if necessary.
Copy linkTweet thisAlerts:
@jdunderhillauthorJan 12.2010 — Thanks for the replies, it has worked like a charm!

Regards

Jamie

?
×

Success!

Help @jdunderhill 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.8,
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,
)...