NumberValue Decimal problem

In short my problem is that I need a value to display on a surface GUI and add +0.05 every time a button is clicked, it works fine except for the fact that it will often display the value as something like 0.4000000000001, despite the number value that’s being added just being 0.05. I want it to just display any number from the hundreds decimal place and up only.

I have looked around and haven’t found any helpful answers and have very little scripting knowledge so any help would be appreciated.
image_2022-09-27_143621760

round the number using math.round(x) when you add to the number itself

1 Like

What you can do is:

local unrounded = 2.8499999999

local rounded = math.round(unrounded * 100) / 100
1 Like

I’m not sure how or where I would insert that
image_2022-09-27_150226685

don’t know if this would error but here you go.

local newvalue = math.round(UpgValue.Value)
UpgValue.Value = newvalue
1 Like

In addition to the other solutions provided, you can use string.format to only display from the hundreds place and above:

TextLabel.Text = string.format("%.2f", value)
4 Likes

Wouldn’t this only work for that one specific value though ? I would have to add hundreds of more values to this

you can try this.

if click then
UpgValue.Value += ClkValue.Value
UpgValue.Value = math.floor(UpgValue.Value)
end
1 Like

It is a bug.

1 Like

It doesn’t seem like this worked for me, or perhaps I just didn’t insert it right, it no longer works after inserting it.
image_2022-09-27_151947158

Honestly just experiment with it, as in try putting math.floor in different ways and see if it works.

You do not need to use math.floor to workaround this. Just be patient and wait for it to be fixed.

math.floor works like math.round but also i did not know about the “bug” so yeah.

That was just an example. Using the code you shared earlier:

local UpgValue = script.Parent.UpgraderInfo.UpgradeValue
local ClkValue = script.Parent.UpgraderInfo.ClickValue
script.Parent.ClickDetector.MouseClick:Connect(function(click)
    if click then
        UpgValue.Value = math.round((UpgValue.Value + ClkValue.Value) * 100) / 100 
    end
end)

This is not a bug and math.round will not solve this problem

@HugeCoolboy2007 has the only correct answer: string.format

The other answers will not do what you want.

This 0.49999999999 behavior is intentional: Behavior change: tostring() will start returning shortest precise decimal representation of numbers