[英]Parsing a string to decimal works on Windows 8 but not on windows 7
我有一个采用字符串值并将其解析为十进制的方法。 在Windows 8.1上可以完美运行,但是当我在Windows 7上测试应用程序时会失败。 Windows 7确实安装了Dot net Framework 4.5。
可能是什么问题呢?
方法如下:
void Save_Details()
{
//The customer enters dots instead of commas to specify a decimal place
string Ammount_Now = textbox1.Text.Replace('.', ',');//value text entered is 123.34
//The value of Ammount_Now is now 123,34
decimal value = 0;
bool Ammount_Good = decimal.TryParse(Ammount_Now, out value);
if(Ammount_Good)
{
//continue with method
// windows 8 returns true in the decimal.TryParse()
// windows 7 returns false in the decimal.TryParse()
// They both use .net framework 4.5
}
else
{
//failed parsing
}
}
操作系统版本没有什么问题,您在具有不同语言环境的机器上测试了代码。
只要解析时使用正确的CultureInfo,也无需替换小数点即可“修复”它们。 如果始终希望使用输入文本.
作为小数点,您应该使用Decimal.TryParse方法(String,NumberStyles,IFormatProvider,Decimal)重载:
decimal.TryParse(Amount_Now,NumberStyles.Number,CultureInfo.InvariantCulture,out value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.