Posted By

Scooter on 05/19/08


Tagged

math


Versions (?)

Ceiling and Floor


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/05/ceiling-and-floor.html

In mathematics, there are two elementary special functions called ceiling() and floor() which allow us to round up or down, respectively, to the nearest whole number. These functions exist natively in PHP, but not in ASP. They are handy in situations where you want to force a number like 15.8 to round down to 15, but the round() function rounds it up to 16 according to the standard rules for rounding.

  1. <%
  2. ' ASP Mathematics Library - Floor and Ceiling Functions
  3. '
  4. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  5. '
  6. ' This work is licensed under the Creative Commons Attribution License. To view
  7. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  8. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  9. ' 94305, USA.
  10.  
  11. ' Returns the largest integer less than or equal to the specified number.
  12. function floor(x)
  13. dim temp
  14.  
  15. temp = Round(x)
  16.  
  17. if temp > x then
  18. temp = temp - 1
  19. end if
  20.  
  21. floor = temp
  22. end function
  23.  
  24. ' Returns the smallest integer greater than or equal to the specified number.
  25. function ceil(x)
  26. dim temp
  27.  
  28. temp = Round(x)
  29.  
  30. if temp < x then
  31. temp = temp + 1
  32. end if
  33.  
  34. ceil = temp
  35. end function
  36. %>

Report this snippet  

You need to login to post a comment.