[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re[2]: Rounding to nearest price fraction?



PureBytes Links

Trading Reference Links

I have three functions I wrote:

_RoundTo rounds to the nearest increment X you specify, up or down,
 whichever is closer.
_RoundUp rounds only up to the nearest X you specify.
_RoundDown rounds only down to the nearest X you specify.

Here is the source code for each function.  Enjoy.

=======================================================================

{function: _RoundTo
 by Alex Matulich, Unicorn Research Corporation, 2 Oct 2003
 Round a number x to the nearest unitsize
 Example: _RoundTo(2.34,0.2) returns 2.4.}

Inputs: x(NumericSimple), unitsize(NumericSimple);

_RoundTo = floor(x/unitsize + 0.5) * unitsize;

=======================================================================

{Function: _RoundUp
 by Alex Matulich, Unicorn Research Corporation, 2 Oct 2003
 Round a number up to the nearest fraction.  For example,
 102.28 rounded up to the nearest 0.25 would be 102.5.}

Inputs: num(NumericSimple), frac(NumericSimple);

_RoundUp = ceiling(num/frac)*frac;

=======================================================================

{Function: _RoundDown
 by Alex Matulich, Unicorn Research Corporation, 2 Oct 2003
 Round a number down to the nearest fraction.  For example,
 102.98 rounded down to the nearest 0.25 would be 102.75.}

Inputs: num(NumericSimple), frac(NumericSimple);

_RoundDown = floor(num/frac)*frac;

=======================================================================

-- 
  ,|___    Alex Matulich -- alex@xxxxxxxxxxxxxx
 // +__>   Director of Research and Development
 //  \ 
 // __)    Unicorn Research Corporation -- http://unicorn.us.com