- Home /
How to keep Unity from rounding floats?
I am using this code to round my time(float) to two decimals. I then show it using a GUI label.
dynamicContent = player.thisleveltime;
dynamicContent = Mathf.Round(dynamicContent * 100f) / 100f;
However, the problem with it is that when the float equals something point zero (ex: 9.0) unity rounds it to 9 with out the decimal point and then immediately adds it back in when it changes to 9.1. What ends up happening is that the decimal point and the digits behind it flash every second. Is there any way to stop this?
Answer by IgnoranceIsBliss · Aug 18, 2014 at 01:05 AM
The problem isn't rounding of the number itself - when you convert a float to a string, it uses the smallest number possible unless you tell it exactly what you want.
So when you convert the number to a string, use something like this...
[Your Float].ToString("0.0");
This would always show one digit and one decimal place - no more, no less.
For more details you could check out http://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx
Your answer
![](https://koobas.hobune.stream/wayback/20220613160841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
float precision, again 0 Answers
Using MathF Lerp on a float, please help! 1 Answer
How to limit the distance to Half of the Circle from its actual raidus 2 Answers
Using Float and Mathf 1 Answer
How to round or convert doubles? 2 Answers