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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Rounding numbers

    Hi All,

    Is there a simple way of doing the following? I am working with percentages by using a basic operator ((number / 365)*100). If the resultant value is below 50% (regardless of how many decimal places there are), the number needs to round down to the nearest whole percent. If the value is above 50%, it should be rounded up to the nearst whole percent.

    I have not been able to find a way. Ultimately, I am looking at including this within a function that does the above opertion to convert it to percentage so I can apply other rules to the result, and not having it as a separate function unless there is no other way. I am using IE6.

    Any help would be appreciated. I have looked into the round method but couldn't find how to tell it do do what I want.

    Cheers,

    A
    Last edited by longman; 06-17-2009 at 01:03 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    var num = 323;
    var a = num/365*100;
    if (a < 50) {
    var result = parseInt(Math.floor(a));
    }
    else {
    var result = parseInt(Math.ceil(a));
    }
    alert (result);
    </script>

    Instructions on a Swedish chainsaw -- "Do not attempt to stop chain with your hands or genitals."

  • Users who have thanked Philip M for this post:

    longman (06-17-2009)

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts
    Thanks Philip, you're a legend!


  •  

    Posting Permissions

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