In a nutshell, I'm wondering why the ToString() lines below don't match my expectations.
float actual = 111253.469F;
double rounded = Math.Round(actual, 2); // 111253.47 (expected)
string stringified = actual.ToString(); // 111253.50 (expected 111253.469)
string currency = actual.ToString("C"); // $111253.50 (expected 111253.47 or possibly .46)
Can someone explain what is going on?
Note: I don't expect the floating point value to be exact, per the other questions on floating point numbers. However I do expect the string output to reflect the string value of the floating point number in question.
In this case, if I set a break point and look at the value of 'actual' in a quick watch, it doesn't display '111253.5', it displays '111253.469'. I don't understand why that wouldn't be the case when printing as well. And especially in the currency case, I would expect it to round to the appropriate decimal point and then display, rather than picking some other literal value. This isn't a math question, it is a ToString() behavior question.
Edit:
Per Quantic's comment below, it appears the ToString
is using the G
format by default which limits the number to 7 significant digits, resulting in the number being seen here.