Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you choose what decimal math.floor and math.ceil round to?

Asked by
ItsMeKlc 235 Moderation Voter
8 years ago

Is there a way to choose what decimal point math.floor/ceil round to? Like make it round to the 100th or 1000th instead?

0
A simple method, if you're comfortable with the math, is to simply say "math.ceil(num * 10) / 10" (for rounding to a single decimal point) chess123mate 5873 — 8y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

No, but you can do some fancy math with powers of ten to get that behavior:

function ceilBase(num, decimals)
    if not decimals then decimals = 0 end
    local pow = 10^decimals
    num = num*pow
    num = math.ceil(num)
    num = num/pow
    return num
end

And the same for math.floor.

Ad

Answer this question