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.
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.
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 ---------------------------------------------------------------------------------------
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.
Comment