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:
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:
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:
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).