Added increment rounding to lume.round()

This commit is contained in:
rxi 2014-03-02 21:29:23 +00:00
parent d344b9d04e
commit f2877dbd6e
2 changed files with 10 additions and 4 deletions

View File

@ -18,9 +18,14 @@ lume = require "lume"
### lume.clamp(x, min, max) ### lume.clamp(x, min, max)
Returns the value `x` clamped between the values `min` and `max` Returns the value `x` clamped between the values `min` and `max`
### lume.round(x) ### lume.round(x [, increment])
Rounds `x` to the nearest integer. Rounds away from zero if we're midway Rounds `x` to the nearest integer; rounds away from zero if we're midway
between two integers. between two integers. If `increment` is set then the number is rounded to the
nearest increment.
```lua
lume.round(2.3) -- Returns 2
lume.round(123.4567, .1) -- Returns 123.5
```
### lume.sign(x) ### lume.sign(x)
Returns `1` if `x` is 0 or above, returns `-1` when `x` is negative. Returns `1` if `x` is 0 or above, returns `-1` when `x` is negative.

View File

@ -15,7 +15,8 @@ function lume.clamp(x, min, max)
end end
function lume.round(x) function lume.round(x, increment)
if increment then return lume.round(x / increment) * increment end
return x > 0 and math.floor(x + .5) or math.ceil(x - .5) return x > 0 and math.floor(x + .5) or math.ceil(x - .5)
end end