forked from rjpcomputing/luaforwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_ext.lua
More file actions
27 lines (23 loc) · 741 Bytes
/
math_ext.lua
File metadata and controls
27 lines (23 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
--- Additions to the math module.
module ("math", package.seeall)
local _floor = floor
--- Extend <code>math.floor</code> to take the number of decimal places.
-- @param n number
-- @param p number of decimal places to truncate to (default: 0)
-- @return <code>n</code> truncated to <code>p</code> decimal places
function floor (n, p)
if p and p ~= 0 then
local e = 10 ^ p
return _floor (n * e) / e
else
return _floor (n)
end
end
--- Round a number to a given number of decimal places
-- @param n number
-- @param p number of decimal places to round to (default: 0)
-- @return <code>n</code> rounded to <code>p</code> decimal places
function round (n, p)
local e = 10 ^ (p or 0)
return _floor (n * e + 0.5) / e
end