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

How would I math.floor with 2 decimals remaining?

Asked by
Aepeus 59
7 years ago

I have a math.floor script which rounds the number and adds SI prefixes at the end, but im trying to figure out to leave 2 decimals remaining. For example 1240.1245435, would be 1.24k+, instead of 1k+. Please help!

local plr = game.Players.LocalPlayer
local lstats = plr:WaitForChild("leaderstats")
local doritos = lstats:WaitForChild("Doritos")
local coinslabel = script.Parent

while true do -- always add bigger numbers first
    wait()

    if doritos.Value >= 1000000000000000000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000000000000000000) .. "o+ Doritos!" -- 1,000,000,000,000,000,000,000,000,000 / 1o+      

    elseif doritos.Value >= 1000000000000000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000000000000000) .. "sp+ Doritos!" -- 1,000,000,000,000,000,000,000,000 / 1sp+   

    elseif doritos.Value >= 1000000000000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000000000000) .. "sx+ Doritos!" -- 1,000,000,000,000,000,000,000 / 1sx+  

    elseif doritos.Value >= 1000000000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000000000) .. "qn+ Doritos!" -- 1,000,000,000,000,000,000 / 1qn+ 

    elseif doritos.Value >= 1000000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000000) .. "qd+ Doritos!" -- 1,000,000,000,000,000 / 1qd+    

    elseif doritos.Value >= 1000000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000000) .. "t+ Doritos!" -- 1,000,000,000,000 / 1t+

    elseif doritos.Value >= 1000000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000000) .. "b+ Doritos!" -- 1,000,000,000 / 1b+

    elseif doritos.Value >= 1000000 then
    coinslabel.Text = math.floor(doritos.Value / 1000000) .. "m+ Doritos!" -- 1,000,000 / 1m+

    elseif doritos.Value >= 1000 then
    coinslabel.Text = math.floor(doritos.Value / 1000 ) .. "k+ Doritos!" -- 1,000 / 1k+

    elseif doritos.Value <= 1000 then
    coinslabel.Text = doritos.Value .. " Doritos!" -- <1,000 / <1k+

    end 
end

1 answer

Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
7 years ago

I think you're looking for a function like this:

local function floor2Decimals(x)
    return math.floor(x / .01) * .01
end
0
math.floor(x / .01) * .00001 would be for 1k? Aepeus 59 — 7y
0
Close, but the goal is to shift the decimal place over to the right to the length of precision you want. This is done in his division operation, where he divides by the number of decimal places you desire. In this case, the tenths place (2 decimals). You simply copy whatever you multiply by, over to whatever you divide by. It cancels out. ScriptGuider 5640 — 7y
0
For 1k: math.floor(x / .00001) * .00001 Validark 1580 — 7y
Ad

Answer this question