double.TryParse has bug ???

man out 0 Reputation points
2024-01-12T06:08:45.4133333+00:00

var number = "422.3262"; double.TryParse(number, NumberStyles.Float, CultureInfo.InvariantCulture, out double value);

When I run the above code on the console program,the result is 422.3262 But when I run it on the asp.net program,the result is 422.32619999999997
Why? double.TryParse has bug ?????

My System info: Microsoft Windows 10 Professional Edition x64 Version 10.0.18363 Internal Version 18363 .NETFramework 4.8.03752

Developer technologies Visual Studio Debugging
Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 5,586 Reputation points
    2024-01-15T19:49:35.89+00:00

    You should give the output value a strong type. Hence this code:

                string mytest = "422.3262";
                double mytestout = 0;
    
                double.TryParse(mytest, NumberStyles.Float, CultureInfo.InvariantCulture, out mytestout);
    
                Console.WriteLine(mytestout);
    
                Console.WriteLine("Hit enter to continue");
                Console.ReadLine(); 
    
    

    In above, running in asp.net, or as a console app, I get the same results: User's image

    However, as pointed out else where, single, double numbers are VERY prone to rounding errors. So, say this:

                double mytestout = 0;
    
                for (int i = 1; i <= 10; i++) {
                    mytestout += 0.1f;
                    Console.WriteLine(mytestout.ToString());            
                }
    
    
                Console.WriteLine("Hit enter to continue");
                Console.ReadLine(); 
    
    

    Now, we going to add JUST 10 simple numbers here. The output is thus this: User's image

    I mean, gee, we can't even add 10 numbers!!! In fact, in computing science class, this was one of the FIRST lessons!! In other words, floating numbers are approximate. So, while my first example with explicit type does work (not sure why yours did not). However, one good way to avoid above issues when writing business or accounting software? Use what are called a "scaled" interger, or a packed decimal. They work different, and thus this code works fine, and does not result in simple rounding errors. Hence:

                decimal mytestout = 0;
    
                for (int i = 1; i <= 10; i++) {
                    mytestout += 0.1m;
                    Console.WriteLine(mytestout.ToString());            
                }
    
    
                Console.WriteLine("Hit enter to continue");
                Console.ReadLine(); 
    
    

    Output: User's image

    This is also very much why when say working with a database, you want to use "Money" type (SQL server), or say MS-access, and use Currency type. Once again, these are packed decimal numbers, and they represent decimal values correctly, but single, double, and float are only approximate values. As noted, above was quite much the first lesson we learned in day one of our computing classes. To many, it is quite a shock that computers mess up after adding just a few simple floating numbers! So, as above shows, rounding errors show up rather easy. So, for general math, in most cases this is not an issue, since the rounding is quite a few decimal points out. However, for numbers involving money values, you have to use integer, or a decimal type (which are equivalent of Currency and Money types that you often see in database systems).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.