Outputting a double number with three decimal places

G

Guest

Hi

I'm really stuck outputting a double number to the console with three
decimal places if the furthest right value is a zero.

I can coutput the number 4.546 as 4.546 but then if I output 0.220 it comes
out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1.

How can I format it to include the zeros?

Thanks
 
J

Jon Skeet [C# MVP]

Andy said:
I'm really stuck outputting a double number to the console with three
decimal places if the furthest right value is a zero.

I can coutput the number 4.546 as 4.546 but then if I output 0.220 it comes
out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1.

How can I format it to include the zeros?

Have you tried using "0.000" as a format specifier? For instance:

using System;

class Test
{
static void Main()
{
Display (0.22);
Display (4.546);
}

static void Display(double d)
{
Console.WriteLine ("{0:0.000}", d);
}
}
 
G

Guest

Jon Skeet said:
Have you tried using "0.000" as a format specifier? For instance:

using System;

class Test
{
static void Main()
{
Display (0.22);
Display (4.546);
}

static void Display(double d)
{
Console.WriteLine ("{0:0.000}", d);
}
}

That is brilliant. It worked a treat. Thank you very much.
 

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

Top