Cannot get Slider's value correctly

I have a “Slider.OnValueChanged” listener that gets the float marked by the slider.
The slider’s value goes from “600” to “9.999998e+08”.

In the function, I have:

 private void HandleSliderValueChanged(float value)
    {
        Debug.Log("VALUE: " + value);

         _myInputField.text = value.ToString("F0");

         _valueText.text = _myInputField.text.ToString("n0");

     
    }

My question is, why _myInputField.text and _valueText.text get different numbers?
_myInputField.text gets “999999799”,
while _valueText.text gets “999,999,800” (= 999999800).

I noticed that the slider max value appears on the “e+0” formatting. Maybe it’s got something to do with the issue, in which case, I don’t know how to solve it.

Perhaps because you gave different formatting instructions to .ToString() ?

Go look at the formatting docs for ToString()… it’s all laid out on Microsoft/MSDN.

Seems like a duplicate issue of: Prevent a large number - "ToString()" to use "E+0" format

They are similar but not duplicates. On that thread, I was asking how to convert from one format to another. On this one, I’m asking about the behavior of the slider’s value and “n0” vs “F0”. I didn’t want to post this question on the previous thread and get someone tell me I’m off-topic and to post a new thread. But if I’m incorrect, then my apologies.

It’s got to be it, thanks. “F0” is getting the correct value, which is “999999799”. I have to delve into how the “n0” format works since it doesn’t work as I expected.

Unfortunately, the “n0” format is not explained here:
https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-7.0

I’ll try to find it somewhere else and report my findings. In the meanwhile, if somebody feels generous and wants to share, I’ll appreciate it.

You can do all your own formatting too, it’s not hard, dividing numbers and printing suffixes. I usually just do exactly what I want to get, such as:

string MyFormat( int x)
{
  if ( x >= 1000000)
  {
     x /= 1000000;
     return x.ToString() + "m";
  }
  if ( x >= 1000)
  {
     x /= 1000;
     return x.ToString() + "k";
  }
  return x.ToString();
}

etc. (I just hand-typed the above, it has never been compiled, so forgive typos)

1 Like

Sorry if the following question is off-topic (I don’t want to create duplicates), now I want to focus on the slider:

The slider max value is set to 999999799 (ended in 799). Then it translates into the “x.xxxe+xx” format, as seen in the inspector (in this case, “9.999998e+08”). if I convert this float value (9.999998e+08) to “long”, I get 999999800, probably because it doesn’t take into account small figures and goes by larger steps.

The only way that occurs to me to get the correct “long” is to convert the “slider max value” to string (string stringValue = _mySlider.value.ToString("F0")) and then get the “long” (long longValue= Convert.ToInt64(stringValue)). This way, I get the expected long (999999799).

However, this is not suitable since I cannot convert this long value into the slider value. If I wanted to subtract one from the long value (longValue--; (999999798)) and then set the slider value to the long value:
_mySlider.value = (float) longValue, the value of the slider doesn’t change, it keeps being the same: 9.999998e+08. When translating the number, it yields 999999799 instead of 999999798. (Same happens if I did _mySlider.value--; directly)

I need the slider to handle large numbers too, is there a way to make the slider use a format that doesn’t use “e+”, or at least a workaround?

All the Slider source code is up on github, you can study and hack it however you like, within the limits of their EULA.

https://github.com/Unity-Technologies/uGUI

Thank you!

Had no luck so far.

I thought of overriding this cs but it seems too overkill, not to mention that probably Unity won’t let me do it that easily. Tomorrow I’ll have a look at another option that I have in mind. I’ll report it if I succeed.

Rather than overwriting the original slider rename the class into a new one and make the necessary changes? I don’t see any internal stuff here so you should be good to go.

1 Like

I can’t access the SetPropertyUtility. Appart from that, everything seems fine.

Here’s the source code of it: uGUI/UnityEngine.UI/UI/Core/SetPropertyUtility.cs at 2019.1 · Unity-Technologies/uGUI · GitHub

Alternatively you could use the UI Toolkit or follow this video.

1 Like

Thank you, sir! This did it.

Now the custom slider handles large numbers precisely.

Thank you all, I’ll mark the thread as resolved.