views:

144

answers:

1

Seems like this should be something straightforward, but I haven't been able to get it right. I've looked at http://idunno.org/archive/2004/14/01/122.aspx for reference.

Example: I would like to print a table of double values, with each double output having 3 decimal precision, and take up 10 spaces (left aligned). Conceptually, I tried something like this, but it only works with precision OR padding, not both:

foreach(line in lines)
{
    foreach (double val in line)
    {
         Console.Write("{0:0.000,-10}", val);
    }

    Console.WriteLine()
}

Update: I can use padleft/padright in very simple scenarios, if i have more complicated output it becomes not very concise. Is there something similar to sprintf?

+7  A: 

Try

double d = 3.14;
Console.WriteLine("{0,10:0.000}", d);

P.S: have a look at this article as a primer on string formatting. Also, string.Format should allow you doing everything sprintf does - and actually more... what else are you trying to do?

Paolo Tedesco
:) very nice! thanksah my fault for not reading the description more carefully
homie347