Decimal value for string conversion

Math.Round((ClosePrice - OpenPrice), 5) = -0.00001 

But when I convert it to tostring, it gives "-1E-05"

 Math.Round((ClosePrice - OpenPrice), 5).ToString() = "-1E-05" 

Why is this so? How can I get "-0.00001"

+4
source share
3 answers

You can use the format specifier as shown on MSDN Standard Number Format Strings

 double foo = -0.00001; Console.WriteLine(foo.ToString("f5")); 
+11
source

ToString() selects a format based on formatting the value to achieve the most compact representation. If you want to select a specific format, you should use the ToString(string format) overload instead. For example, if you call

 Math.Round((ClosePrice - OpenPrice), 5).ToString("N5") 

as a result, you get the string "-0.00001" .

+4
source

Each class inheriting from object (and therefore any class) has a .ToString() method. What it outputs depends on whether it was overwritten, and if so, how it was rewritten (that is, what the developer wanted to implement as a string). The same process that you would perform when implementing the .ToString() method for one of your classes.

This applies to โ€œWhyโ€ - โ€œHowโ€ others answered.

+2
source

Source: https://habr.com/ru/post/1386635/


All Articles