- Oct 9, 2017
- 504
- 584
Unfortunately, yes.Regional settings are the coders nightmare, but in this case, simply replacing "," with "." should to the trick (and pray no thousand separator was used). Or am I oversimplifying here?
I tried this too and then another region does not work anymore >_<
So , what i did was creating a static function which checks what float conversion actually works.
(What a shitshow i have to say)
Looks something like this :
C:
private static bool? FloatCommaUseinsteadtofPoint = null;
public static bool StringToFloatRegionFree(string cmd,out float Value)
{
//what a shitshow....
if (FloatCommaUseinsteadtofPoint == null)
{
//first step, try out which conversion works
string commaSeperated = "1,4";
string pointSeperated = "1.4";
float CommaSep = -1.0f;
float PointSep = -1.0f;
float.TryParse(commaSeperated, out CommaSep);
float.TryParse(pointSeperated, out PointSep);
//check which one actually makes it work
if (CommaSep == 1.4f)
FloatCommaUseinsteadtofPoint = true;
if (PointSep == 1.4f)
FloatCommaUseinsteadtofPoint = false;
}
float returnvalue = -9999.99f;
bool conversionSucceded = false;
//now make the real conversion using the knowledge from the above shitshow.
if(FloatCommaUseinsteadtofPoint != null && FloatCommaUseinsteadtofPoint == true)
{
conversionSucceded = float.TryParse(cmd.Replace(".", ","), out returnvalue);
if (conversionSucceded)
{
Value = returnvalue;
return true;
}
}
if (FloatCommaUseinsteadtofPoint != null && FloatCommaUseinsteadtofPoint == false)
{
conversionSucceded = float.TryParse(cmd.Replace(",", "."), out returnvalue);
if (conversionSucceded)
{
Value = returnvalue;
return true;
}
}
//Make it really bad to show that the conversion did not succed
print($"WARNING , float conversion failed, not able to parse {cmd} into something useful.");
Value = -9999.99f;
return false; ;
}