I’m having some troubles with a FloatField element and setting its formatString property.
In the FloatField the group and decimal separators do not correspond to what I have as my regional culture settings.
FloatField f = root.Q<FloatField>();
// Value I see in the field: "12,345.68"
f.value = 12345.678f;
f.formatString = "N2";
// Value I see printed out : "12 345,68"
Debug.Log("Value is: " + f.value.ToString("N2"));
I tried modifying the FloatField value on runtime to check if the formating had taken effect and got the same result.
Hi,
The FloatField uses InvariantCulture to format the value. So it won’t take into account the current culture.
You could create a class that inherits from FloatField and override the ValueToString method.
Thanks for the quick response. So this is intended behavior? I would expect them to use the users culture. Will there be a property we can set to define what culture to use on FloatFields in the future?
Its something we are discussing internally. We will likely improve it in the future when we have tighter localization support. For now you can either inherit or try setting the formatString property.
formatString gets passed into the ToString method:
I’m a bit confused now. In summary you suggested two options to deal with the problem:
1 - Create a class the inherits from FloatField and override ValueToString() to have the behavior I want.
2 - Set the formatString property.
Did I understand correctly? As far as I know I did try option 2 and it didn’t work, I described as much in the initial post. Am I missing some different way to set formatString?
Your first post assumes that the formatString will use the current culture, it won’t. It will always use invariant culture when it does the formatting. So change f.value.ToString(“N2”)) to use the invariant culture and you should get the same results.
I see. My problem is not that they are different, it’s that I don’t want FloatField to use invariant culture. The debug statement was a way to validate that my format string wasn’t fundamentally wrong.
Thanks anyways for the information. Do the docs reflect anywhere that the formatString on FloatFields will use invariant culture? I couldn’t find it anywhere.
Im not sure. If you see something missing from the docs you can use the report a problem button at the bottom of the page, click the problem, add more info and type in the text box. It will file a bug report with the docs team.