Hi I need to only have 1 decimal place in my number but I can seem to get this function to work. e.g I need 20.9 but when it cycles through the mathf.Movetowards function I get 18.9999999 etc etc. Anyway here is my code. Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class TestDecimal : MonoBehaviour { public float default_O2Value = 20.9f; public float currentOxygenValue; float O2Value; float recoveryTime; public Text Oxygen_Value; void Update () { currentOxygenValue = Mathf.MoveTowards(currentOxygenValue, O2Value, recoveryTime * Time.deltaTime); Oxygen_Value.text = currentOxygenValue.ToString(); // trying to use System.Math.Round(val , 1) but cant get it to work //Oxygen_Value.text = System.Math.Round(currentOxygenValue.ToString(), 1.0f); //Cant make it work } }
1) Math.Round takes a double and int, not a string and float. So that second commented out line is a no go. 2) always remember that floats will always be prone to small error. This is because it's stored in both binary and has a finite number of digits. The issue that arises is that values like .1 (or a multiple there of, like .9) are repeating values. 0.1 dec ~ 0.0001100110011... 0.9 dec ~ 0.11100110011... But since a float can only store a finite number of digits, as opposed to an infinite repeating series, the value ends up always being just shy of the expected result. Which is where the float error comes from (your .999999). 3) Rounding 18.99999 to one place won't giver you 18.9, it'll give you 19. It'll end up rounding up. You'll want to floor to one decimal place if you expect 18.9 here you can find my FloorTo method which gives flooring to a specific place: https://github.com/lordofduct/space.../blob/master/SpacepuppyBase/Utils/MathUtil.cs Code (csharp): /// <summary> /// FloorTo some place comparative to a 'base', default is 10 for decimal place /// /// 'place' is represented by the power applied to 'base' to get that place /// </summary> /// <param name="value"></param> /// <param name="place"></param> /// <param name="base"></param> /// <returns></returns> /// <remarks></remarks> public static float FloorTo(float value, int place, uint @base) { if (place == 0) { //'if zero no reason going through the math hoops return (float)Math.Floor(value); } else { float p = (float)Math.Pow(@base, place); return (float)Math.Floor(value * p) / p; } } Of course float error is an issue. 4) ToString allows format strings, try something like: Code (csharp): Oxygen_Value.text= currentOxygenValue.ToString("#0.0"); Of course the actual value won't be that, but the displayed value will. This format string says: # - if there is a digit in this place or larger, show it, otherwise nothing. first 0 - always show a digit, if no digit, show 0 . - show the decimal point second 0 - always show the first decimal place, if no decimal value there, show 0
awesome thanks lordofduct, that fixed it Oxygen_Value.text= currentOxygenValue.ToString("#0.0"); Thanks so much for your help. Im a bit new in the forums, how do you mark this thread as solved ?
Marking a thread as solved isn't a part of the culture here. We just call a thread done once people stop commenting on it. Some people will put solved in the title, but it's not needed.