Need guidance on math.floor

Hello guys,
I just started to learn about math.floor.

Why the decimal number on the first output line ends with 1. The calculation for this is (number / number). On the second output line, it ends with 2. The calculation for this is (number / number * 100). And with math.floor. It will just round up the numbers to the nearest number.

Screenshot 2024-03-18 083554

not really sure what you’re asking, but math.floor is like rounding but it rounds to the lowest nearest integer:

math.floor(8.9) -- output 8
math.floor(8.1) -- output 8
math.floor(9.1) -- output 9

TL;DR Computers can’t store numbers 100% accurately

Hello!
math.floor() rounds a given number down to the nearest integer. For example, if you have a number like 6.37, math.floor(6.37) returns 6. It essentially chops off any decimal portion of the number, leaving only the integer part.

The issue of float the calculating arises due to floating-point precision in computer arithmetic. Floating-point numbers can’t always represent decimal values precisely. For instance, 0.1 + 0.2 will be 0.30000000000000004 might actually be stored as a slightly different value due to the limitations of floating-point representation. Therefore, when performing calculations with such numbers, slight inaccuracies can occur, leading to unexpected results.

In some situations it will be useful to use math.floor or reduce the float precision programmatically to avoid such issues!

The calculation.

local number = calculation(playerLevel.Value / level)
				local Percentage = number * 100
				
				PercentageLabel.Text = math.floor(calculation(Percentage)).."%"

Didn’t quite understand your question, but I can say that math.floor rounds up a number to the lowest nearest one, or simplier explanation: just removes the numbers after dot. The another way of rounding up is math.ceil, it’s same to the math.floor except it rounds up to the closest highest number, or simplier: remove the numbers after dot, add 1 to it.
About strange percentages: computers struggle to store exact numbers.
If you’re trying to make an actual round up, I think that you will need to combine math.floor and math.ceil using if, additionaly you can read documentation about it.

Math.floor() just removes the digits after your number
Crazy right? :exploding_head:

For example we got some scary number “10.5” we want to make it a nice number a “10” we call the hero of this case which is math.floor it will go like a superman find the evil creature (the number after the dot) kill them all and return the good people “10”

1 Like

If you want to get the first two digits of a number you can use string.format i dont remember exactly but i think it will be

string.format("%.2f", number)
1 Like