-1

Unity version: 2020.3.11f1c1 issue: ‘float.Parse’ reported an error on some users’ computers Error message: FormatException: Input string was not in a correct format.System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Single.Parse (System.String s) (at <695d1cc93cca45069c528c15c9fdd749>:0)

Code: float val = float.Parse("0.5"); Of course, the "0.5" here is in the static data table

This problem only occurs in some players’ clients, and most players can run normally

DesDev
  • 3
  • 2
  • 1
    `float.Parse` is *culture dependent*: decimal separator can vary from cultute to culture (e.g. `"0,5"` - comma - in Russian, `"0.5"` - dot - in US). You can put it as `float val = float.Parse("0.5", CultureInfo.InvariantCulture);` or specify some culture, e.g. `float val = float.Parse("0.5", CultureInfo.GetCultureInfo("en-US"));` – Dmitry Bychenko Dec 27 '21 at 08:39
  • Nex to specifying CultureInfo, also look into float.TryParse. Then you get a return value instead of an exception – Hans Kesting Dec 27 '21 at 10:51

1 Answers1

1

You need to provide the culture explicitly since depending on the users region the string may be interpreted differently.

You can do this using CultureInfo.InvariantCulture

float.Parse("0.5",CultureInfo.InvariantCulture)

You may also run into the same issue with other things, like DateTime.ToString for example. This is a common issue experienced by people when developing with Unity.

See: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture?view=net-6.0

Thomas Maw
  • 405
  • 4
  • 10