So I am making a game where you are a volleyball player and you train to get overall. The ova(ovverall) sometimes goes to 16.000000001 when I want it to be 16.1 or 16 but I dont know how to do that. Plus I have search the whole browser I think… Can anybody help? Even thoght each time I add to ova I add 0.1 I dont know how it like goes to 16.0000001
This is simply floating point imprecision. The numbers are stored in binary, so it cannot represent some decimal values with perfect precision. For internal calculations this is irrelevant, just remember to never compare floating point values to some hardcoded other value, as they likely wont be the exact same (instead you could compare approximately).
Dont round the value for internal usage.
When you display it on the GUI, simply format it there, for example using string formatting.
Just chose and pick how many decimal places you want to see, whether always or only if they exist, and so on.
I would recommend to watch this Computerphile video on floating point numbers to learn about the basics.
Here’s code that will hide away the fractional:
var text = number.ToString("F0");
If you want to see the fractional only if it’s there:
var text = (number % 1 == 0) ? number.ToString("F0") : number.ToString("F1");
Where do I put that… Like where I change the text or when I asign the value? where
I’m not sure if you want “16.00” or “16.0” or just “16” returned, but whenever I have this issue I just multiply by (16.0)10 then round, or (16.00) multiply by 100 then round, and then just divide by 10 or 100 to get my result. But if you only want “16” to show but still have “16.79” to be the internal, then just make two separate variables(actual vs show).
I’m also sure there’s more fancy ways of doing it, but simplistically does it for me.