簡體   English   中英

將字符串解析為十進制在Windows 8上有效,但在Windows 7上不行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM