/    Sign up×
Community /Pin to ProfileBookmark

Help with Rounding Up – My first post

Hi all – My first post.

I am developing a simple web page form where the visitor can input height and width and the script calculates the area and price.

The script below does this fine.

However, I would like to roundup the answer to the next integer for example if price = 45.678 final output would be 46.

I have tried Math.round(x) but the syntax totally defeats me.

I would be grateful if someone would give me a hand and post the required change to my script below.

Hopefully not a big ask.

Regards


————————-

[I]<script type=”text/javascript” language=”Javascript”>

var h1 = 0;
var w2 = 0;
var price = 0;

function calculate () {
// The Calculation: change operator in this equation to change the calculation
price = h1 * w2 * .0005;

var elem;
elem = document.getElementById(“h1”);
elem.value = h1;
elem = document.getElementById(“w2”);
elem.value = w2;

elem = document.getElementById(“price”);
elem.value = price;
}

function setValue(elem) {
var val = 0.0;
if (elem.value != “”) {
val = parseFloat(elem.value);
}
else {
val = 0.0;
}
switch (elem.id) {
case “h1”: h1 = val; break;
case “w2”: w2 = val; break;
}
calculate();
}
</script>[/I]


————–

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PadonakMay 17.2013 — elem = document.getElementById("price");
elem.value = Math.round(price);
Copy linkTweet thisAlerts:
@i-did-thisauthorMay 17.2013 — Padonak

Many thanks for such a quick reply.

Where exactly does your addition fit in my script.

Ta
Copy linkTweet thisAlerts:
@PadonakMay 17.2013 — there is only one place in your script where you set document.getElementById("price") value, am i wrong?
Copy linkTweet thisAlerts:
@i-did-thisauthorMay 17.2013 — Panonak

Many thanks for your help - All appears to be working as required - and yes there is only one place where "price" is set - Regards
Copy linkTweet thisAlerts:
@PadonakMay 18.2013 — good to hear it helped ))
×

Success!

Help @i-did-this 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.15,
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,
)...