There is a double variable of type d = 0.122312332453. Tell me, please - how to set the output in the console of a specified number of decimal places?

2 answers 2

For example, by formatting a string with String.Format (string, object). Console.WriteLine (string, object) is the same, only immediately shows to the console.

The following code shows a number and 2 decimal places:

double d = 0.12312332453; Console.WriteLine("{0:0.##}", d); 
  • This method will not return three decimal places in the number 1.5997, but round it to 1.6. Is there a way to return exactly 1.599? - Jiraiya
 double num = 321.345678; Console.WriteLine("{0:0.00}", num); // выведет два знака после запятой 
  • 2
    In fact, if you really need a fixed number of decimal places, this answer is better: ideone.com/hpfn3M - VladD
  • thanks, I will know) - DreamChild