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

How would I round down a decimal with code?

Asked by 4 years ago

Basically how do I turn 9.12761656 to 9.12? I tried using math.floor but it turns it into an integer. Thanks!

2 answers

Log in to vote
1
Answered by 4 years ago

One way would be to multiply it by 100, floor that, then divide it by 100.

9.12761656 * 100 = 912.761656
floor of 912.761656 = 912
912 / 100 = 9.12

Hope this helped

0
I've created a function for this, so you are right too, but I have a feeling mine is easier to use. CrastificeDude612 71 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I've seen this question before. Here's a quick problem-solver. I've created a function that rounds a long decimal into a short decimal:

local function decimalRound(num,round)
    -- Checking if both variables are valid numbers, otherwise throws an error.
    assert(tonumber(num),"Variable num is not a number.")
    assert(tonumber(round),"Variable round is not a number.")

    -- Calculations
    local result = num
    local roundNum = 10^round
    result *= roundNum

    result = math.floor(result)
    result /= roundNum

    -- Return the "result" variable
    return result
end

Example of using decimalRound():

local x = decimalRound(9.12761656,2)
print(x)

The following example would return:

9.12

No problem, from Canada. :)

0
what does assert mean?? CaIcuIati0n 246 — 4y
0
^ whenever the first parameter of assert is false or nil, it errors the second parameter brokenVectors 525 — 4y

Answer this question