4.9 displaying as 4.8999999999 on GUI?

Hi, recently I’ve had a problem with GUI where trying to display decimals just does not work. In my game, money can have decimal points, but whenever a decimal is displayed, instead of displaying 4.9 for example, it displays ‘4.8999999999999,’ to the point where it goes off of the screen and makes it impossible to see how much money you actually have.

image

Your money is stored on the server in a NumberValue, and whenever it gets changed a RemoteEvent gets fired to the player to update the GUI.

This is the code running on the server side:

game.ReplicatedStorage.serve.OnServerEvent:Connect(function(player, p, t, liking)
	print(p)
	if p == 1 then
		p1off = true
		game.Workspace.p1values["t"..t.."stock"].Value -= 1
		game.Workspace.p1values.money.Value += (3+(liking/10))
		game.ReplicatedStorage.serve:FireClient(player, (3+(liking/10)))
		player.leaderstats.Money.Value = game.Workspace.p1values.money.Value
	end
end)

And this is the code running on the client:

game.ReplicatedStorage.serve.OnClientEvent:Connect(function(got)
	game.Players.LocalPlayer.PlayerGui.register.serve.Visible = false
	game.Players.LocalPlayer.PlayerGui.moneypop.Enabled = true
	game.Workspace.purchase:Play()
	game.Players.LocalPlayer.PlayerGui.moneypop.StudsOffset = Vector3.new(0, 0, 0)
	game:GetService("TweenService"):Create(game.Players.LocalPlayer.PlayerGui.moneypop, TweenInfo.new(3), {StudsOffset = Vector3.new(0, 4, 0)}):Play()
	game.Players.LocalPlayer.PlayerGui.moneypop.serve.Text = "+$"..got
	game.Players.LocalPlayer.PlayerGui.moneypop.back.Text = "+$"..got
	game.Players.LocalPlayer.PlayerGui.ScreenGui.money.Text = "$"..game.Workspace["p"..p.."values"].money.Value
	wait(3)
	game.Players.LocalPlayer.PlayerGui.moneypop.Enabled = false
end)

Thanks in advance!

That has to do with Floating Point Imprecision, which the only thing you can really do is to round the number to the amount you want it to be.

A Common Example of this, is when you add 0.1 + 0.2 and try to check if its equal to 0.3.

You will have to math.floor the end value. This is mainly because of rounding issues with precision numbers.

Alright, thanks. Is there a way that I could round the number to the nearest 0.1, or will I have to just round it to the nearest whole?

You can divide the rounded value by 0.1 then multiply by 0.1 to achieve this.

(math.floor(num) / 0.1) * 0.1

1 Like

The simple way would be to multiply the number to be whole, and then divide it after you round the number

lets say you have the number 4.89, multiply by 10, you will get 48.9, you can then round the number as a whole, which will give you 49, you then divide by 10, which will give you 4.9

You can do the Inverse of the Multiplication and the Division here to get the same Result, by Dividing the Number by 0.1, and Multiply by 0.1, but this comes with more precision issues

Edit: math.round() will give you a result of 49, and will change depending on the decimal point, and its amount, math.ceil() will always give you 49 no matter the decimal’s amount (ceil rounds up), and math.floor() will give you 48, no matter the deimals amount (floor rounds down).

2 Likes

Yeah alight that makes sense. Thank you for helping!

You can use string formatting to set how many decimal places to show. This example will use two decimal places i.e. “123.00” if 123, and “12.35” if 12.345

game.Players.LocalPlayer.PlayerGui.ScreenGui.money.Text = string.format("$%.2f", game.Workspace["p"..p.."values"].money.Value)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.