Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to remove useless "decimal digits"?

Asked by
Pengdoo 26
7 years ago
Edited 6 years ago

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

Log in to vote
1
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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.

0
Thank you! I'll do just that! Gosh, this is going to save me some trouble. Thanks again! Pengdoo 26 — 7y
Ad
Log in to vote
5
Answered by 7 years ago

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

0
Thanks! I also approve this answer too! Thank you! Pengdoo 26 — 7y

Answer this question