Formatting doubles

  • Thread starter Thread starter Homer Simpson
  • Start date Start date
H

Homer Simpson

Hi,

I'm looking for a way to format the output of my double value. For example,
the result of a calculation may be 12.3456789 but I want to display 12.346
(three decimal places of precision). How do I do this?


Thanks,
Scott
 
The ToString() method can be immensely useful when wanting to format a
numeric value. To do so, simply pass in your formatting string as a parameter
when calling ToString(), take the following example:

double x = 12.3456789;

Console.WriteLine(x.ToString("0")); //12
Console.WriteLine(x.ToString("0.0")); //12.3
Console.WriteLine(x.ToString("0.00")); //12.35
Console.WriteLine(x.ToString("0.000")); //12.346

http://msdn.microsoft.com/library/d...emglobalizationnumberformatinfoclasstopic.asp
gives some more details on some of the other options you can use when
specifying a formatting string.

Brendan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top