Difference between revisions of "Ceiling and Floor"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>Slserpent
m (Ceil Floor moved to Ceiling and Floor)
imported>Tantalus
m
Line 6: Line 6:




If you want a number to be rounded normally, just typecast by turning the variable into a short or long.
If you want a number to be rounded down, just typecast by turning the variable into a short or long.


  float fNumber
  float fNumber
  short iNumber
  short iNumber
   
   
  set fNumber to 9.45
  set fNumber to 1.99
  set iNumber to fNumber
  set iNumber to fNumber
  ;iNumber is now 9
  ;iNumber is now 1





Revision as of 00:43, 27 August 2006

Here's how to get the ceiling and floor of a float variable. Floor is truncating the decimal portion (rounding down). Ceiling is rounding up the variable.

Take the number 9.45:
The floor is 9.
The ceiling is 10.


If you want a number to be rounded down, just typecast by turning the variable into a short or long.

float fNumber
short iNumber

set fNumber to 1.99
set iNumber to fNumber
;iNumber is now 1


It's only a bit more involved to get the floor. You basically just mod out the decimal portion and subtract.

float fNumber
short iNumber

set fNumber to 9.45
set iNumber to fNumber - (fNumber % 1)
;iNumber is now 9


And now the ceiling.

float fNumber
short iNumber

set fNumber to 9.45
set iNumber to (fNumber - (fNumber % 1)) + 1
;iNumber is now 10