Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 6 of 6
Thread: Rounding decimals

07052013, 07:52 PM #1
 Join Date
 Jul 2013
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Rounding decimals
I am trying to get 10% of standardPremium.
Say 500.5 is entered for standardPremium and the answer is 50.050000000000004
I want it round to 50.05.
How would I do this?
Code:var excessPremium_Percent = 0.1; var input = function (standard_premium,incurred_losses) { var standard_premium = document.getElementById("SPnum").value; var incurred_losses = document.getElementById("ILnum").value; document.getElementById("beginning_funds").innerHTML = standard_premium*excessPremium_Percent; return standard_premium; return incurred_losses; };
07052013, 08:20 PM
#2
But you have a major problem in your function code:Code:document.getElementById("beginning_funds").innerHTML = ( standard_premium*excessPremium_Percent ).toFixed(2);
You can't return two different values from a single function.
When you doonly the *FIRST* return statement returns anything. THE FUNCTION ENDS IMMEDIATELY with that return.Code:return standard_premium; return incurred_losses;
And you also made an error in the declaration of your function:
Because you used var to declare NEW VARIABLES standard_premium and incurred_losses the declaration of those as arguments to your function (in red) is IGNORED!Code:var input = function (standard_premium,incurred_losses) { var standard_premium = document.getElementById("SPnum").value; var incurred_losses = document.getElementById("ILnum").value;
Finally, what is the point is getting the value of incurred_losses when you never use it?
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
07052013, 09:28 PM
#3
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,459
 Thanks
 0
 Thanked 632 Times in 622 Posts
You can return as many values as you like from a function as long as you return them as either an array or as an object.
For example:
return [standard_premium, incurred_losses];
or
return {sp: standard_premium, il: incurred_losses};
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
07052013, 09:40 PM
#4
Yes, but either of those is still only a single "value" in the sense that the array or object is a single unit. I didn't bother mentioning that because I figured this was a real newbie, given the other errors and the level of the question.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
07052013, 11:46 PM
#5
 Join Date
 May 2012
 Location
 France
 Posts
 216
 Thanks
 0
 Thanked 29 Times in 27 Posts
After these offtopic developments and judgements and to answer specifically to the question, there is two methods to round a number with two decimals : Math.round(myValue*100)/100 and myValue.toFixed(2).
The first one gives a number as result with no more as two decimals.
The second gives a String with two zero or nonzero digits after the points.
07062013, 12:02 AM
#6
Yes, Julien, but from the context of his code:
It was pretty clear that he wanted the innerHTML to display as two decimal places.Code:document.getElementById("beginning_funds").innerHTML = standard_premium*excessPremium_Percent;
If he used your first version of rounding, then if the answer happened to be 57.00 it would only display as 57, with no decimals. So the mathematical rounding doesn't do any good for display purposes.
So I *did* answer him:And the rest of my answer may have been irrelevant to his question, but I don't think it is "off topic" with regards to the rest of his JavaScript coding.Code:document.getElementById("beginning_funds").innerHTML = ( standard_premium*excessPremium_Percent ).toFixed(2);
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.