Hello everyone I have a problem with rounding number of my score
{English is not my first language}
I made a clicker game and I did script with " Gold per second ". So when I my score is changing it become like that : 1.00002 or 4.000078. and I want to be like that : 1 or 4. Please help !! if you need more details ask me.
if you want to displayed integers then cast your values to an int
- without seeing your code it's difficult to be more explicit.
Answer by jgodfrey · Jul 03, 2016 at 04:10 PM
In a simple division, if both values are INTs, the result will be an INT (so, just a whole number). If one or both values are FLOAT, the result will be FLOAT.
It'd be easier to answer this with some actual code to comment on, but...
I assume you are doing something like this:
int gold = 10;
float time = 2.4f;
float goldPerSec = gold / time;
Debug.Log(goldPerSec); // Prints 4.166667
In the above, since the "time" variable is a float, the result of the division (10 / 2.4) will also be a float (4.166667). If you want just the INT portion of that value, simply cast the result to an INT, and be sure to store the result in an INT. So...
int gold = 10;
float time = 2.4f;
int goldPerSec = (int)(gold / time);
Console.WriteLine(goldPerSec); // Prints 4
Answer by El-Deiablo · Jul 04, 2016 at 02:47 AM
If your trying to round a string you can use:
float.ToString("F2"); - This rounds 2 decimal places - F1 would be one decimal place...etc
There's also mathf.round - https://docs.unity3d.com/ScriptReference/Mathf.Round.html