[当我尝试在实际设备上将字符串解析为float时,它成功,但在模拟器上失败。两者都在iOS 13.1上。例如,“ 81,5”在实际设备上转换为81.5,但在仿真器上转换为815
public static float StrToFloatDef(string value, float defValue)
{
value = value.Replace('.', ',');
float floatValue;
if (float.TryParse(value.Trim(), out floatValue) == false)
floatValue = defValue;
return floatValue;
}
你是对的杰森。
我用逗号作为分隔符,现在可以正常使用了
float.TryParse(value.Trim(), NumberStyles.Any, new NumberFormatInfo() { NumberDecimalSeparator = "," }, out floatValue)
您可能没有将两个设备设置为相同的本地化。有些使用句点作为小数点分隔符,有些使用逗号。
float.Parse(value, CultureInfo.InvariantCulture.NumberFormat);