/    Sign up×
Community /Pin to ProfileBookmark

Rouding to two positions behind the comma?

Sorry, i already asked a simular question about this topic yesterday, but i still couldn’t figure it out.
I have a form that calculates prices, but sometimes i get more than tow positions behind the comma. I know how i can round, but i don’t know how i can round to two positions behind the comma!?
If you choose a one in all three menus in the following code you will see my problem(the result is: 389.40000000000003).

<html>
<head>

<script language=”JavaScript” type=”text/JavaScript”>
function price() {
var two = parseFloat(window.document.form1.menu1.value);
var three = parseFloat(window.document.form1.menu2.value);
var four = parseFloat(window.document.form1.menu3.value);
var five = two + three + four;
window.document.form1.textfield.value = five;
}
</script>
</head>

<body>
<form name=”form1″>
<select name=”menu1″ size=”1″ onChange=”price()”>
<option value=”0″>0</option>
<option value=”129.80″>1</option>
</select>
<br>
<select name=”menu2″ size=”1″ onChange=”price()”>
<option value=”0″>0</option>
<option value=”129.80″>1</option>
</select>
<br>
<select name=”menu3″ size=”1″ onChange=”price()”>
<option value=”0″>0</option>
<option value=”129.80″>1</option>
</select>
<input type=”text” name=”textfield”>
</form>

<br>
</body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJan 15.2004 — Hi!

If you alter your function like this, it should work:

function price() {

var two = parseFloat(window.document.form1.menu1.value);

var three = parseFloat(window.document.form1.menu2.value);

var four = parseFloat(window.document.form1.menu3.value);

var five = (two + three + four).toFixed(2);

window.document.form1.textfield.value = five;

}

Cheers - Pit
Copy linkTweet thisAlerts:
@requestcodeJan 15.2004 — Does "toFixed()" actually round or just fix it to two decimal positions? I thought you would have to use the round() method of the Math object. for example:


var five=(Math.round((two + three + four) * Math.pow(10,2)))/100
Copy linkTweet thisAlerts:
@mortyauthorJan 15.2004 — Thanks (Danke?) Pittimann, now it works!!

But i read, that toFixed need ie5.5 or higher to work. So the idea of requestcode might be usefull in some cases.
Copy linkTweet thisAlerts:
@requestcodeJan 15.2004 — That is much easier than my example if you want to round to two numbers. Thanks.
×

Success!

Help @morty 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...