Issues with displaying numbers

Hello! I’ve been having a pretty bad issue with extra decimals appearing. Such as 4.80000000000001 kind of things. Here’s my code:

script.Parent.Text = "Mash Multiplier:                  "..(math.floor(game.Players.LocalPlayer.upgrades.MashMulti.Value)/10+1).." > "..(math.floor(game.Players.LocalPlayer.upgrades.MashMulti.Value)/10+1.1).." - ✨"..(game.Players.LocalPlayer.upgrades.MashMulti.Value * 25 + 25).." Gems✨"
1 Like

Sorry for the bad formatting. Any help is appreciated!

Have your entire math operation be inside the math.floor function.

I’ve tried, I just get a whole number for the display. I need it to be able to display the first decimal place.

time it by 10, do math.floor, then divide by 10 ?

The issue is not really solvable while keeping it a number, since that’s being caused by a floating point error, but what you could do, is do something like

local Stringnum = string.sub(tostring(math.round(number*10)/10), 1, 4)

(forgot a “)”)

Really confused as to what’s happening here.

so it’s rounding to the nearest 1 decimal, then, converting it into a string, and finally gets the first 4 characters of the string(so it’s limited to 4 total digits)

ah i have the multiplication and division messed up lemme fix it real fast

I think you just need to use the standard string formatting convention that is outlined here.

E.g.

local FORMAT = "Mash Multiplier:                  %:.3f"

script.Parent.Text = string.format(FORMAT, value)

I still am really confused. Never often used string.sub or string.format.

Oh right I saw someone use that like a month ago, entirely slipped my mind though

Okay not a problem. I’ll outline explicitly how this would work for your example and try to explain it in a bit more detail.

local MASH_MULTI_DISPLAY_FORMAT = "Mash Multiplier:                  %.3f > %.3f - ✨%.3f Gems✨"

local currentMulti = math.floor(game.Players.LocalPlayer.upgrades.MashMulti.Value)/10+1
local nextMulti = math.floor(game.Players.LocalPlayer.upgrades.MashMulti.Value)/10+1.1
local newGems = game.Players.LocalPlayer.upgrades.MashMulti.Value * 25 + 25

script.Parent.Text = string.format(
    MASH_MULTI_DISPLAY_FORMAT,
    currentMulti,
    nextMulti,
    newGems
)

So where I have written %.3f in the format string (MASH_MULTI_DISPLAY_FORMAT) will be where the numbers are placed. The .3f means 3 decimal places. There are more flags / options which are listed in the documentation.

You’ll get the hang of it more easily by trying it out yourself.

1 Like

You made that very easy to understand.

1 Like

So to be clear though, .1f would be one decimal place, correct?

1 Like

Yeah that is correct. The . signifies that it is decimal places. The 1 (or 3) specifies the number of places. The f means it expects a floating point number as an input.

1 Like

Thank you so much again, this really helps. I’m going to be messing around with this for a while probably.

1 Like