Reputation: 7840
Math.Round((ClosePrice - OpenPrice), 5) = -0.00001
But When I convert it into tostring it gives "-1E-05"
Math.Round((ClosePrice - OpenPrice), 5).ToString() = "-1E-05"
Why this is so ? How can I get "-0.00001"
Upvotes: 4
Views: 736
Reputation: 21
Every class inheriting from object
(and therefore any class) has a .ToString()
method. What it outputs depends on weather it was overwritten and if it was, how it was overwritten (i.e. what did the implementer want to give out as string. Its the same process you would go through if implementing a .ToString()
method for one of your classes.
This is as to "Why" - the "How" has been answered by others.
Upvotes: 2
Reputation: 727027
ToString()
chooses a format based on the value being formatted to achieve the most compact representation. If you would like to choose a specific format, you need to use the ToString(string format)
overload instead. For example, if you call
Math.Round((ClosePrice - OpenPrice), 5).ToString("N5")
you will get "-0.00001"
string as the result.
Upvotes: 4
Reputation: 1949
You can use format specifier as demonstrated on MSDN's Standard Numeric Format Strings
double foo = -0.00001;
Console.WriteLine(foo.ToString("f5"));
Upvotes: 11