| View previous topic :: View next topic |
| Author |
Message |
soulphalanx
Joined: 22 Aug 2005 Posts: 35
|
Posted: Tue Aug 23, 2005 4:18 am Post subject: Rounding Decimals |
|
|
| Can anyone tell me how to round down or round up a decimal number? |
|
| Back to top |
|
 |
Benihana
Joined: 31 Jul 2005 Posts: 12
|
Posted: Tue Aug 23, 2005 4:29 am Post subject: |
|
|
Programming in Lua
rounding functions (floor, ceil)
Wikipedia entry for floor and ceil
floor: for a real number x, floor(x) is the largest integer less than or equal to x.
ceil: for a real number x, ceiling(x) is the smallest integer no less than x. |
|
| Back to top |
|
 |
soulphalanx
Joined: 22 Aug 2005 Posts: 35
|
Posted: Tue Aug 23, 2005 4:50 am Post subject: |
|
|
| luaplayer doesnt recognize floor(X) and ceiling(X) |
|
| Back to top |
|
 |
Benihana
Joined: 31 Jul 2005 Posts: 12
|
Posted: Tue Aug 23, 2005 4:59 am Post subject: |
|
|
That's because you didn't follow the link and look at the way it was used... it is part of the math library, therefore implentation is as follows:
local x = math.ceil(2.3)
print(x) --> 3 |
|
| Back to top |
|
 |
Nils
Joined: 03 Aug 2005 Posts: 16
|
Posted: Thu Aug 25, 2005 3:42 am Post subject: |
|
|
try that.
| Code: | function math.round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
|
x = 8.13
math.round(x) = 8
y = 8.52
math.round(y) = 9 _________________ Puzzle Bobble - The arcade port! |
|
| Back to top |
|
 |
|