Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to remove useless "decimal digits"?

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.

2 answers

✓ Accepted answer

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.

1 comment

0

Thank you! I'll do just that! Gosh, this is going to save me some trouble. Thanks again!

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

1 comment

0

Thanks! I also approve this answer too! Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.