Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    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;
    
    };

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Code:
        document.getElementById("beginning_funds").innerHTML = 
            ( standard_premium*excessPremium_Percent ).toFixed(2);
    But you have a major problem in your function code:

    You can't return two different values from a single function.

    When you do
    Code:
        return standard_premium;
        return incurred_losses;
    only the *FIRST* return statement returns anything. THE FUNCTION ENDS IMMEDIATELY with that return.

    And you also made an error in the declaration of your function:
    Code:
    var input = function (standard_premium,incurred_losses)
    {
        var standard_premium = document.getElementById("SPnum").value;
        var incurred_losses = document.getElementById("ILnum").value;
    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!

    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.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    After these off-topic 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 non-zero digits after the points.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, Julien, but from the context of his code:
    Code:
    document.getElementById("beginning_funds").innerHTML = standard_premium*excessPremium_Percent;
    It was pretty clear that he wanted the innerHTML to display as two decimal places.

    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:
    Code:
    document.getElementById("beginning_funds").innerHTML = 
        ( standard_premium*excessPremium_Percent ).toFixed(2);
    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.
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •