mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added increment rounding to lume.round()
This commit is contained in:
parent
d344b9d04e
commit
f2877dbd6e
11
README.md
11
README.md
@ -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.
|
||||||
|
3
lume.lua
3
lume.lua
@ -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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user