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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Rounding off decimal places

    Hi,
    I have a script that does a great job of calculations of numbers. However, it calculates to 333.867756656 sometimes. How can I make the script round off the last 2 digits to make it 333.87 during the calculation?

    Here is the following code:

    <script type="text/javascript">
    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! http://javascript.internet.com -->
    <!-- Created by: www.jdstiles.com -->
    <!-- Begin
    function startCalc(){
    interval = setInterval("calc()",1);
    }
    function calc(){
    one = document.form1.Total_Premium.value;
    two = document.form1.Documentation_Fee.value;
    three = document.form1.VAT.value = ((one * 1) + (two * 1)) * ((14 / 100) * 1) ;
    document.form1.Total_Due.value = (one * 1) + (two * 1) + (three * 1) ;
    P1 = document.form1.Premium_Dealer.value = (one * 1) * ((12.5 / 100) * 1) ;
    P2 = document.form1.Premium_CheckPlan.value = (one * 1) * ((24 / 100) * 1) ;
    P3 = document.form1.Premium_Natsure.value = (one * 1) * ((7.5 / 100) * 1) ;
    P4 = document.form1.Premium_QSales.value = (one * 1) * ((7.5 / 100) * 1) ;
    P5 = document.form1.Premium_Reserves.value = (one * 1) * ((40.5 / 100) * 1) ;
    P6 = document.form1.Premium_Assupol.value = (one * 1) * ((2 / 100) * 1) ;
    document.form1.Premium_Agents.value = (one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1) ;
    }
    function stopCalc(){
    clearInterval(interval);
    }
    // End -->
    </script>

    Many thanks,

    Chris

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    Code:
    document.form1.Premium_Agents.value = Math.round((one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1)*100)/100;

    this takes " (one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1) " and multiplies it by 100.
    we do this to move the decimal place over 2. then we round the oucome number off using Math.round(). after rounding the number off, we then divide it by 100, which moves the decimal place back over 2, giving you a perfect rounded 2 decimal place number.
    Last edited by brandonH; 12-06-2006 at 11:47 AM.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    You could also use toFixed(),
    i.e.
    myIntVar.toFixed(2);

    http://www.pageresource.com/jscript/j_a_03.htm

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    I have experienced to many problems with using toFixed() lol. but yes that is another way.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Yeah, I didn't realize how crappy it was in IE. Never mind.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brandonH View Post
    ...multiplies it by 100.
    we do this to move the decimal place over 2. then we round the oucome number off using Math.round(). after rounding the number off, we then divide it by 100, which moves the decimal place back over 2, giving you a perfect rounded 2 decimal place number.
    I don't wish to be critical of your method, but is there no way to do this properly? Sensibly, the round function would have a 2nd argument where you could say at which level rounding should take place.

    Coming from Rexx, where you never have to worry about numbers, Javascript is an unpleasant shock.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I've had this snippet for a few years, not sure if it is still any good


    PHP Code:
    <script type="text/javascript">
    <!--
    function 
    roundOff(valueprecision){
    value "" value
    precision 
    parseInt(precision)

    var 
    whole "" Math.round(value Math.pow(10precision))
    var 
    decPoint whole.length precision

    if(decPoint != 0){
    result whole.substring(0decPoint)
    result += "."
    result += whole.substring(decPointwhole.length)
    }
    else{
    result whole
    }

    return 
    result
    }
    //-->
    </script> 
    Last edited by Mr J; 12-08-2006 at 07:58 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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