I'm using C# as my language and I wrote this custom Parse float routine for my application
public double ParseFloat(string value)
{
Regex regex = new Regex(@"[+-]?[0-9][0-9]*.[0-9][0-9]*");
value = Regex.Replace(value, @",(?<=\d,)(?=\d)", "").Trim();
if (value.ToLower() == "false")
value = "0.0";
else if (value.ToLower() == "true")
value = "1.0";
if (regex.IsMatch(value))
{
if (value == "")
return 0.0;
else
return float.Parse(value);
}
else if (value == "")
return 0.0;
else
{
value += ".0";
return ParseFloat(value);
}
}
I wrote this because I need a way to convert from string to float in more complex ways. Someone is beta testing my application and apparently this function is causing the application to stop, because the line "float.Parse(value)". It says that value is malformed.
This could be a comma vs period issue but I tried testing float.Parse("25,0") and this went through. Is it possible that a french version of Windows 11 doesn't work with "25.0" despite the fact that on my machine 25.0 and 25,0 both seem to work fine.
There are a lot of different "Parse" methods in .Net. Ints, doubles, floats, DateTimes, etc. All of them have an optional parameter for which Culture you'd like to use for the parsing. If you don't specify one, it's going to default to the computer's Culture that it's currently running on.
For example, with this code:
float val = float.Parse("1 234,56");
If you run this on a French operating system, or any other computer who's default Culture uses commas for the decimal separator, then it will execute just fine. However, on a US computer, or another computer that uses some other character for the decimal separator, then it will throw an exception.
This is by design. Always specify a culture.
You can do the Invariant Culture (more-or-less US English), or you can get one by country/language.
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
float val = float.Parse("1 234,56", culture);
// val will be "one thousand, two hundred, and thirty four, and fifty six hundredths".
This code will work on any computer around the world. Just remember, if you specify a culture (and you should), you can only parse values applicable for that culture. If you need to support multiple parsing cultures, you need to put in a bit more work to allow that.