Round Up/Down Function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Dermot
    Indigo Rose Customer
    • Apr 2004
    • 1791

    Round Up/Down Function

    Here is a handy function I converted from one of my Access apps that lets you round a number to any fractional percision you want. You can round up or down by passing a positive or negatve second parameter.

    Code:
    function RoundUpDown(varNumber, varRoundTo)
    
        -- --------------------------------------------------
        -- --------------------------------------------------
        -- Comments  :
        -- Parameters: varNumber
        --             varRoundTo -
        -- Returns   : Number -
        -- --------------------------------------------------
        --Name:          RoundUpDown Function
        --Purpose:       Round varNumber to varRoundTo, up or down
        --Inputs:        varNumber - number to round
        --               varRoundTo - rounding percision
        --               +varRoundTo - rounds up
        --               -varRoundTo - rounds down
        --Example        RoundUpDown(5.12, 0.25) = 5.25
        --               RoundUpDown(5.12, -0.25) = 5.00
        --Output         varNumber rounded up/down
        -- --------------------------------------------------
        -- --------------------------------------------------
        
        --if no second paramter was passed in then set it to number 1
        varRoundTo = varRoundTo or 1
        
        -- Check to see if a valid number was passed in
        if String.ToNumber(varNumber) == 0 then
        	-- invalid number passed in so stop and return 0
        	return 0
        end
        
        -- Check to see if a valid roundto number was passed in
        if String.ToNumber(varRoundTo) == 0 then
        	-- invalid number pass in so set varRoundTo to 1 and continue
        	varRoundTo = 1
        end
        
        -- check to see if the varNumber is already rounded to the desired percision
        varTemp = (varNumber / varRoundTo)
    
        if Math.Floor(varTemp) == varTemp then
        	-- varNumber is already round to the desired percision so return varNumber
            return varNumber
        else
        	-- perform the rounding and return the result
            return Math.Floor(((varNumber + (2 * varRoundTo)) / varRoundTo) - 1) * varRoundTo
        end
        
    end
    
    ---------------------------------------------------------------------------------------
    Use like this.

    result = RoundUpDown(1.23, 0.25)
    returns 1.25

    An example of how this can be very usefull is if you are calculating the quantity of materials required for a job, lets say floor tiles. Lets say you can only buy the tiles in boxes of 25. Now lets say your floor requires 258 tiles. Now you can't buy exactly 258 tiles so we RoundUpDown(258, 25) which gives us 275.

    This has served me well for many years and I hope some people find it usefull.
    Dermot

    I am so out of here :yes
  • Lorne
    Indigo Rose Staff Member
    • Feb 2001
    • 2729

    #2
    Rounding to a precision is a really useful function!

    I use a similar function in C++ to round values to the nearest base.

    C++ code:
    Code:
    // rounds a double value to the nearest base (precision)
    //
    // Examples: round_to(x, 1.);  // nearest whole
    //           round_to(x, .01); // nearest hundredth
    //           round_to(x, .5);  // nearest half
    //           round_to(x, 3);   // nearest multiple of three
    double round_to(const double n, const double precision)
    {
    	ASSERT(precision);
    	return floor(n / precision + 0.5) * precision;
    }
    Note that the function assumes correct input values (i.e. you must not pass 0 for precision).

    Here's a lua version:

    Code:
    function RoundTo(n, precision)
    	return Math.Floor(n / precision + 0.5) * precision;
    end
    This is similar to your function, but it doesn't always round values up to the next nearest multiple.

    I suspect you could accomplish that with a simpler calculation, though. Off the top of my head I would try this:

    Code:
    function NextNearestMultiple(n, precision)
    	return Math.Ceil(n / precision) * precision;
    end
    ...which I think will produce the same results as your function, minus the error checking of course.
    --[[ Indigo Rose Software Developer ]]

    Comment

    • Dermot
      Indigo Rose Customer
      • Apr 2004
      • 1791

      #3
      Code:
      function NextNearestMultiple(n, precision)
      	return Math.Ceil(n / precision) * precision;
      end
      Thanks Lorne. That does to give the same results as mine and is shorter. I just took what I had in VB and converted it to Lua. I knew it worked so I did not want to mess with it. I like your better though.:yes
      Dermot

      I am so out of here :yes

      Comment

      • holtgrewe
        Indigo Rose Customer
        • Jul 2002
        • 779

        #4
        Thanks guys, I think I can use this in my project for calculating price breaks.

        Comment

        Working...
        X