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

how would i round up instead of down with math.floor?

Asked by
luaa233 37
4 years ago
local num = 10.6
0
math.floor(num + 0.5) User#26971 0 — 4y
0
If this helped, be sure to put [Solved] in the title so that people know you don't need any more help. User#26971 0 — 4y
0
You can also use string.format to round to any place: local function round(x, n) return tonumber(string.format("%."..n.."f", x)) end User#26971 0 — 4y
0
Aether, > = math.floor(0.5) = 0, math.ceil(0.5) = 1 KDarren12 705 — 4y
View all comments (5 more)
0
you're forgetting about ceil KDarren12 705 — 4y
0
Did you actually read my comment...? User#26971 0 — 4y
0
math.floor(0 + 0.5) -> 0, math.floor(0.2 + 0.5) -> 0, math.floor(0.5 + 0.5) -> 1, and, finally, math.floor(0.9 + 0.5) -> 1. User#26971 0 — 4y
0
It works exactly as math.round would work if it existed (math.floor(num + 0.5), that is). User#26971 0 — 4y
0
Did you not read what he wrote,, KDarren12? TheeDeathCaster 2368 — 4y

1 answer

Log in to vote
2
Answered by
KDarren12 705 Donator Moderation Voter
4 years ago

You could use math.ceil().

local num = math.ceil()
0
That rounds up to 1 even if the number is, say, 0.3. That's not technically rounding. User#26971 0 — 4y
0
I would downvote this answer, but it's not entirely inaccurate (and there's a chance it is what he was looking for). User#26971 0 — 4y
0
k whtver KDarren12 705 — 4y
0
It IS the answer to his question. math.ceil() rounds up instead of down like math.floor(). If he asked how to round to nearest number, then that would be a different story. sleazel 1287 — 4y
Ad

Answer this question