The problem isn't with the function
Rounding to the nearest integer with just a floor function is usually achieved with the same strategy: floor(x + 0.5), where floor represents some mathematical floor function which rounds its input to the nearest integer less than or equal to the input and x represents some number.
As a proof of concept, notice that numbers with decimal portions just above and equal to 0.5 get rounded down to the next integer (this is because we add 0.5 to them), while numbers with decimal portions just below 0.5 get rounded down to the previous integer.
Floating point numbers
I suspect that the problem is with how you're storing the value. A NumberValue stores floating point numbers; a simple way of thinking of it is as decimals. You can read more about floating point numbers and their intricacies here or you can search for it online. The simple explanation is that what is actually stored is not guaranteed to be absolutely equal to what was attempted to be stored.
IntValue
One solution is to change your storage format. If you're only looking to store integers, then use an IntValue instead.
Moving where the rounding happens
But what if that is unacceptable and you want to keep a floating point representation? What if there are times where you need to store numbers that are not integers?
If that is the case, I would end up moving where the rounding happens: instead of rounding before storing the value, round right before you end up using it. In other words, set the text label to the rounded result.
1 comment
Can u give an example by any chance of what it should look like?