简体   繁体   中英

Scientific Notation - bug in .NET?

Yeah I know it's hard to believe - bug in .NET? But run this code in the command line app:

decimal x;

x = decimal.Parse("3.E-2", NumberStyles.Float);
Console.WriteLine(x);

x = decimal.Parse("5.72e9", NumberStyles.Float);
Console.WriteLine(x);

x = decimal.Parse("3.E−2", NumberStyles.Float);
Console.WriteLine(x);

I'm getting exception during the last parse. Saying:

System.FormatException : Input string was not in a correct format.

The value for the first and last parse are exactly the same. Tested on two different x64 machines compiled toward .NET 3.5 and 4.

Any idea what is going on? Any work-around?

The first and 3rd are not exactly the same.

in the 3rd you have a "longer" - sign, as a result it doesnt know what to do with it. Therefore you would need to check for that and replace it with the standard minus sign

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