By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,178 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,178 IT Pros & Developers. It's quick & easy.

Outputting a double number with three decimal places

P: n/a
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
Jan 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Andy <An**@discussions.microsoft.com> wrote:
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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 5 '06 #2

P: n/a


"Jon Skeet [C# MVP]" wrote:
Andy <An**@discussions.microsoft.com> wrote:
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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


That is brilliant. It worked a treat. Thank you very much.
Jan 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.