简体   繁体   English

将字符串解析为十进制在Windows 8上有效,但在Windows 7上不行

[英]Parsing a string to decimal works on Windows 8 but not on windows 7

I have a method that takes a string value and parses it into decimal. 我有一个采用字符串值并将其解析为十进制的方法。 That works perfectly on windows 8.1 but when i test the application on windows 7 it fails. 在Windows 8.1上可以完美运行,但是当我在Windows 7上测试应用程序时会失败。 Windows 7 does have Dot net Framework 4.5 installed. Windows 7确实安装了Dot net Framework 4.5。

What could be the problem? 可能是什么问题呢?

Here's the method: 方法如下:

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
     }     

}

There's nothing wrong with the OS versions, you tested the code in machines with different locales. 操作系统版本没有什么问题,您在具有不同语言环境的机器上测试了代码。

There's also no need to replace decimal marks to "fix" them as long as you use the proper CultureInfo when parsing. 只要解析时使用正确的CultureInfo,也无需替换小数点即可“修复”它们。 If the input text is always expected to use . 如果始终希望使用输入文本. as the decimal mark, you should use the Decimal.TryParse Method (String, NumberStyles, IFormatProvider, Decimal) overload: 作为小数点,您应该使用Decimal.TryParse方法(String,NumberStyles,IFormatProvider,Decimal)重载:

decimal.TryParse(Amount_Now,NumberStyles.Number,CultureInfo.InvariantCulture,out value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM