mathf.round problem
Trying to make a game so that when I click, its adds 1 but when I pass 10, there are 6 decimals after the number instead of one. Need help!
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Main : MonoBehaviour {
public float clicks = 0;
public float autoClicks = 0;
public int finger = 10;
public GameObject clicker;
void Update ()
{
Text text = clicker.GetComponent<Text> ();
text.text = "" + clicks;
clicks = mathf.round(clicks * 10f) / 10f;
clicks += autoClicks * Time.deltaTime;
if (Input.GetButtonDown ("Press")) {
clicks += 1;
}
if (Input.GetButtonDown ("Press")) {
if (clicks > finger) {
clicks -= finger;
finger += 10;
autoClicks += 1;
}
}
}
}
Is there a particular reason for using floats, rather than ints, for the clicks and autoClicks parameters? It seems like you always want those to be whole numbers, and floats are not guaranteed to be whole numbers, even when you think they should be. Floating point rounding errors are an extremely common headache and many developers prefer working with ints when possible for this very reason.
because Time.deltatime is a float and if i use them as a int I will get an error.
Answer by Bunny83 · Sep 17, 2015 at 08:39 AM
The problem is most likely the order of execution of your code.
You first display the current "clicks" number, then you round it, then you add the autoClicks. Since your autoClicks is multiplied by deltaTime it will be a small value with a lot decimal points. You want to reverse those three lines:
clicks += autoClicks * Time.deltaTime;
clicks = mathf.round(clicks * 10f) / 10f;
text.text = "" + clicks;
You also want to put those lines at the end of your Update method. Any changes that are applied to "clicks" should happen before you round it.
edit
Furthermore there's another potential problem with that approach. Since you round the actual clicks counter you "distort" the actual value. At a high framerate it would be possible that your autoclicks addition might have no affect at all if it's too small and is going to be rounded away each frame.
You should seperate the logic (the actual counting) from the display of the number. So only round it when displaying the number but leave the actual counter alone.
clicks += autoClicks * Time.deltaTime;
text.text = "" + (mathf.round(clicks * 10f) *0.1f);
For other ways to output a formatted string see @Helliums answer.
Answer by Hellium · Sep 17, 2015 at 07:17 AM
If you look at the documentation
http://docs.unity3d.com/ScriptReference/Mathf.Round.html
You will see that this function returns a float. Thus, there may be some imprecisions.
I see two simple options :
Use Mathf.RoundToInt function
Format the output string value using string.format( "{0:#}", clicks ) ;
EDIT : Integers won't be a good solution in fact, since you seem to make a clicking game with auto-clicks, floating values will be necessary.
Your answer
![](https://koobas.hobune.stream/wayback/20220612034023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mathf.Clamp does not work at all. 1 Answer
Int not getting rounded data 0 Answers
Save object position after drag? 0 Answers
Mathf.Pow alternative function? 1 Answer