471,603 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

Outputting a double number with three decimal places

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
2 2666
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


"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.

Similar topics

3 posts views Thread by Danny Woolston | last post: by
2 posts views Thread by David Casey | last post: by
6 posts views Thread by Simon | last post: by
3 posts views Thread by Madan | last post: by
14 posts views Thread by cj | last post: by
248 posts views Thread by md | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.