Results 1 to 5 of 5

Math Help - rounding equation

  1. #1
    Newbie
    Joined
    Jan 2010
    Posts
    3

    rounding equation

    hey guys,

    i feel really incompetent here. i cant come up with a solution that rounds any number UP to the nearest .25.

    Right now, let num be my number (a currency value) that I want to round up to the nearest quarter to yield result...no matter how much closer it is to the lower quarter. For example, 1.80 needs to be 2.00, not 1.75.

    Right now, my equation rounds up & down

    num/25 = mid
    truncate mid to only two decimal places
    mid*25 = result

    does anyone have a good idea how to tweak this to always round up?
    im thinking I need to add something to num or maybe multiply by 100 first to make it an integer, and then divide later to get it back to decimals...but nothing is working out for me.

    Thanks in advance.
    Last edited by cardet; January 4th 2010 at 03:07 PM. Reason: using the word 'equation' wrong...not needing an equation...need to use equations to achieve a solution
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Aug 2007
    From
    USA
    Posts
    3,111
    Thanks
    2
    Try

    1) Multiply by 4 <== Where did that come from?

    2) Add just short of your machine value of 1. I used 0.9999999999. It did NOT work when I used 0.999999999999999. It may take some experimentation to find the right value.

    3) Truncate to integer

    4) Divide by 4.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jan 2010
    Posts
    3
    Quote Originally Posted by TKHunny View Post
    Try

    1) Multiply by 4 <== Where did that come from?

    2) Add just short of your machine value of 1. I used 0.9999999999. It did NOT work when I used 0.999999999999999. It may take some experimentation to find the right value.

    3) Truncate to integer

    4) Divide by 4.

    Worked for me beautifully. Thank you very much!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor
    Joined
    Aug 2007
    From
    USA
    Posts
    3,111
    Thanks
    2
    What value of "almost 1" did you use? ...and in what environment?

    You were really close with the "/25" and then "*25" version. Notice that 1/(0.25) = 1*4. Look familiar? You just had the scale off a bit.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Jan 2010
    Posts
    3
    Quote Originally Posted by TKHunny View Post
    What value of "almost 1" did you use? ...and in what environment?
    I took it out to 10 decimal places (probably didn't need so much precision)

    0.9999999999


    I used this to do an update in a SQL server database. Had an issue where prices of something needed to be rounded UP to the nearest quarter. T-SQL has a ROUND function where you can specify to how many digits. My formula looked like this when I was done:

    (ROUND ((field*4)+.9999999999, 0)) / 4

    It worked beautifully. Just embed that in a UPDATE statement and watch it fly. I'm using a Windows box.


    I remembered an old trick you can do to round...almost worked for what I was after: pick your interval (my case 25), multiply by the reciprocal (why I divided by 25), truncate to two decimal places, and multiply the interval back in.
    Last edited by cardet; January 5th 2010 at 03:46 PM. Reason: saw more in the post that I wanted to respond to
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Rounding up or down averages?
    Posted in the Statistics Forum
    Replies: 1
    Last Post: July 4th 2011, 11:40 AM
  2. Replies: 3
    Last Post: October 18th 2010, 09:48 AM
  3. rounding decimals
    Posted in the Algebra Forum
    Replies: 3
    Last Post: January 22nd 2009, 08:11 AM
  4. rounding up
    Posted in the Algebra Forum
    Replies: 3
    Last Post: November 1st 2008, 01:08 PM
  5. Rounding
    Posted in the Algebra Forum
    Replies: 5
    Last Post: September 11th 2006, 07:16 PM

Search Tags


/mathhelpforum @mathhelpforum