CFLib.org – Common Function Library Project

RoundUpDown(input[, multiple][, direction])

Last updated June 27, 2002

author

Casey Broich

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Rounds a number up or down to the nearest specified multiple.

Return Values:
Returns a numberic value

Example:

<CFOUTPUT>
RoundUpDown(102): #RoundUpDown(102)#<BR>
RoundUpDown(102,10,"up"): #RoundUpDown(102,10,"up")#<BR>
RoundUpDown(102,10,"down"): #RoundUpDown(102,10,"down")#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
input Number you want to round. Yes
multiple Multiple by which to round. Default is 5. No
direction Direction to round. Options are Up or Down. Default is Up No

Full UDF Source:

/**
 * Rounds a number up or down to the nearest specified multiple.
 * 
 * @param input      Number you want to round. (Required)
 * @param multiple      Multiple by which to round.  Default is 5. (Optional)
 * @param direction      Direction to round.  Options are Up or Down.  Default is Up (Optional)
 * @return Returns a numberic value 
 * @author Casey Broich (cab@pagex.com) 
 * @version 1, June 27, 2002 
 */
function RoundUpDown(input){
  var roundval = 5;
  var direction = "Up";
  var result = 0;

  if(ArrayLen(arguments) GTE 2) 
    roundval = arguments[2];
  if(ArrayLen(arguments) GTE 3) 
    direction = arguments[3];
  if(roundval EQ 0) 
    roundval = 1;

  if((input MOD roundval) NEQ 0){
     if((direction IS 1) OR (direction IS "Up")){
      result = input + (roundval - (input MOD roundval));
     }else{
      result = input - (input MOD roundval);
     }
  }else{
    result = input;
  }
  return result;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson