简体   繁体   中英

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 7 does have Dot net Framework 4.5 installed.

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. 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(Amount_Now,NumberStyles.Number,CultureInfo.InvariantCulture,out value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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