Hi guys! So today I need to know why...
print(1.001*50-50)
outputs :
0.049999999999997
Why can't it simply output 0.05? I'm working on a game, and I'm using this to display text on the screen, and I think it'll be frustrating if the players just see 0.049999999999997 instead of 0.05.
Any ideas of how to fix this, please? I would really appreciate it! Thanks!
~Pengdoo.
Theres a math
function library inside of roblox. It contains a floor function, which you can use to floor values. If you want to get 2 digits, you can do the following:
local DecimalValue = math.floor((Value * 100) + 0.5) / 100
for 1 decimal, you would use 10 instead of 100, same for 3 decimals, 1000 instead of 100.
The reason why i do the + 0.5 is so that it will be rounded instead of floored. (0.444 will become 0.44, 0.445 will become 0.45)
If you need any more information, feel free to comment below.
You could use a simple rounding function
function RoundNum(Number,RoundBy) return math.floor((Number/RoundBy)+0.5)*RoundBy end
and for this example, you could round by 0.01
, and if you were to do
print(RoundNum(1.001*50-50,0.01))
it would return 0.05
instead of 0.049999999999997