Rounding Decimals

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
soulphalanx
Posts: 35
Joined: Mon Aug 22, 2005 7:48 am

Rounding Decimals

Post by soulphalanx »

Can anyone tell me how to round down or round up a decimal number?
Benihana
Posts: 12
Joined: Sun Jul 31, 2005 4:54 am

Post by Benihana »

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.
soulphalanx
Posts: 35
Joined: Mon Aug 22, 2005 7:48 am

Post by soulphalanx »

luaplayer doesnt recognize floor(X) and ceiling(X)
Benihana
Posts: 12
Joined: Sun Jul 31, 2005 4:54 am

Post by Benihana »

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
Nils
Posts: 16
Joined: Wed Aug 03, 2005 2:27 pm

Post by Nils »

try that.

Code: Select all

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!
Post Reply