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

Formatting right justified percentages with Console.writeline()

P: n/a
Hello Everyone

How do you format format numbers right-justified using Console.WriteLine(), i.e
I need to line up numbers in vertical columns and the MSDN documentation is pretty poor
Here is the problem

double percent_00_01 = 1D / 10000D
double percent_00_10 = 1D / 1000D
double percent_01_00 = 1D / 100D
double percent_10_00 = 10D / 100D

double percent_00_01_x = percent_00_01
double percent_00_10_x = percent_00_10
double percent_01_00_x = percent_01_00
double percent_10_00_x = percent_10_00

percent_00_01_x *= 100
percent_00_10_x *= 100
percent_01_00_x *= 100
percent_10_00_x *= 100

Console.WriteLine("\n-------------------Multiply by 100--------------------------\n")
Console.WriteLine(" Percent 00 01 = {0, 5:F} % ", percent_00_01_x )
Console.WriteLine(" Percent 00 10 = {0, 5:F} % ", percent_00_10_x )
Console.WriteLine(" Percent 01 00 = {0, 5:F} % ", percent_01_00_x )
Console.WriteLine(" Percent 10 00 = {0, 5:F} % ", percent_10_00_x )
Console.WriteLine("\n-------------------Try all formats -------------------------\n")
Console.WriteLine(" Percent 01 00 = {0, 5:C} ", percent_01_00 )
//nsole.WriteLine(" Percent 01 00 = {0, 5:D} ", percent_01_00 )
Console.WriteLine(" Percent 01 00 = {0, 5:F} ", percent_01_00 )
Console.WriteLine(" Percent 01 00 = {0, 5:G} ", percent_01_00 )
Console.WriteLine(" Percent 01 00 = {0, 5:N} ", percent_01_00 )
Console.WriteLine(" Percent 01 00 = {0, 5:P} ", percent_01_00 )
//nsole.WriteLine(" Percent 01 00 = {0, 5:X} ", percent_01_00 )
Console.WriteLine("\n--------------------Problem with P formatter ---------------\n")
Console.WriteLine(" Percent 00 01 = {0, 5:P} ", percent_00_01 )
Console.WriteLine(" Percent 00 10 = {0, 5:P} ", percent_00_10 )
Console.WriteLine(" Percent 01 00 = {0, 5:P} ", percent_01_00 )
Console.WriteLine(" Percent 10 00 = {0, 5:P} ", percent_10_00 )
Console.WriteLine("\n------------------------------------------------------------\n")

The commented out lines above that throw exceptions
I'm trying the custom numeric formats but keep getting exceptions there too
Multiplication by 100 is not elegant
Is there a better way

Thanks in advance for posting a good solution in this excellent forum :-
Shawn K

PS. I have looked at all the MSDN docs and it takes forever to find useful examples to clone
It was faster to experiment, write this code and post this question

Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
shawnk <an*******@discussions.microsoft.com> wrote:
How do you format format numbers right-justified using
Console.WriteLine(), i.e. I need to line up numbers in vertical
columns and the MSDN documentation is pretty poor.


Try this:

using System;

class Test
{
static void Main()
{
Console.WriteLine ("{0,6:##0.00}", 54.6d);
Console.WriteLine ("{0,6:##0.00}", 100.0d);
Console.WriteLine ("{0,6:##0.00}", 0.3d);
}
}

I think that's what you're after - let me know if it's not.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a

Jon,

Thanks for you input since I can use it in other code.
It does not, however, give me what I am looking for.

The target context is an application that generates a
report of 'counting metrics' where the measurements are always
a percentage on something. So... instead of pie charts we
use tables for console based QandD apps.

Number values in the report are all around 1.0 -> 0.001

Your initial solution;

Console.WriteLine (" {0,7:##0.000}", 0.006d);
Console.WriteLine (" {0,7:##0.000}", 0.546d);
Console.WriteLine (" {0,7:##0.000}", 1.00d);
Console.WriteLine ("");

is similar to;

Console.WriteLine (" {0,7:F3}", 0.006d);
Console.WriteLine (" {0,7:F3}", 0.546d);
Console.WriteLine (" {0,7:F3}", 1.00d);
Console.WriteLine ("");

What I would like is for the 'P' formatter to be like the 'C' formatter
in the code below. I do not want to have to use a temporary value
holder (my_original_value * 100) and the 'F' formatter since I don't
want to write additional code for the temp_value_holder.

Is there an additional formatter mechanism like 'P' or 'C' that
actually works for a percentage centric numeric value context?

This could just be a bug with the 'P' formatter (?!).

Console.WriteLine("-----#--------------------------numeric value context-----------");
Console.WriteLine (" {0,6:##0.00}", 54.6d);
Console.WriteLine (" {0,6:##0.00}", 100.0d);
Console.WriteLine (" {0,6:##0.00}", 0.3d);
Console.WriteLine ("");
Console.WriteLine (" {0,7:##0.000}", 0.006d);
Console.WriteLine (" {0,7:##0.000}", 0.546d);
Console.WriteLine (" {0,7:##0.000}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----F--------------------------F similar to #------------------");
Console.WriteLine (" {0,7:F3}", 0.006d);
Console.WriteLine (" {0,7:F3}", 0.546d);
Console.WriteLine (" {0,7:F3}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----G-----------------------Brainless L/R text alignment-------");
Console.WriteLine (" {0,6:G2}", 0.006d);
Console.WriteLine (" {0,6:G2}", 0.546d);
Console.WriteLine (" {0,6:G2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:G2}", 0.006d);
Console.WriteLine (" {0,-6:G2}", 0.546d);
Console.WriteLine (" {0,-6:G2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----C-----------------------Dot centric L/R text alignment-----");
Console.WriteLine (" {0,6:C2}", 0.006d);
Console.WriteLine (" {0,6:C2}", 0.546d);
Console.WriteLine (" {0,6:C2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:C2}", 0.006d);
Console.WriteLine (" {0,-6:C2}", 0.546d);
Console.WriteLine (" {0,-6:C2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----P--------------------------P not like C :-O ---------------");
Console.WriteLine (" {0,6:P2}", 0.006d);
Console.WriteLine (" {0,6:P2}", 0.546d);
Console.WriteLine (" {0,6:P2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:P2}", 0.006d);
Console.WriteLine (" {0,-6:P2}", 0.546d);
Console.WriteLine (" {0,-6:P2}", 1.00d);
Console.WriteLine ("");

Output is;

-----#--------------------------numeric value context-----------
54.60
100.00
0.30

0.006
0.546
1.000

-----F--------------------------F similar to #------------------
0.006
0.546
1.000

-----G-----------------------Brainless L/R text alignment-------
0.006
0.55
1

0.006
0.55
1

-----C-----------------------Dot centric L/R text alignment-----
$0.01
$0.55
$1.00

$0.01
$0.55
$1.00

-----P--------------------------P not like C :-O ---------------
0.60 %
54.60 %
100.00 %

0.60 %
54.60 %
100.00 %
Nov 16 '05 #3

P: n/a

Jon,

Thanks for you input since I can use it in other code.
It does not, however, give me what I am looking for.

The target context is an application that generates a
report of 'counting metrics' where the measurements are always
a percentage on something. So... instead of pie charts we
use tables for console based QandD apps.

Number values in the report are all around 1.0 -> 0.001

Your initial solution;

Console.WriteLine (" {0,7:##0.000}", 0.006d);
Console.WriteLine (" {0,7:##0.000}", 0.546d);
Console.WriteLine (" {0,7:##0.000}", 1.00d);
Console.WriteLine ("");

is similar to;

Console.WriteLine (" {0,7:F3}", 0.006d);
Console.WriteLine (" {0,7:F3}", 0.546d);
Console.WriteLine (" {0,7:F3}", 1.00d);
Console.WriteLine ("");

What I would like is for the 'P' formatter to be like the 'C' formatter
in the code below. I do not want to have to use a temporary value
holder (my_original_value * 100) and the 'F' formatter since I don't
want to write additional code for the temp_value_holder.

Is there an additional formatter mechanism like 'P' or 'C' that
actually works for a percentage centric numeric value context?

This could just be a bug with the 'P' formatter (?!).

Console.WriteLine("-----#--------------------------numeric value context-----------");
Console.WriteLine (" {0,6:##0.00}", 54.6d);
Console.WriteLine (" {0,6:##0.00}", 100.0d);
Console.WriteLine (" {0,6:##0.00}", 0.3d);
Console.WriteLine ("");
Console.WriteLine (" {0,7:##0.000}", 0.006d);
Console.WriteLine (" {0,7:##0.000}", 0.546d);
Console.WriteLine (" {0,7:##0.000}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----F--------------------------F similar to #------------------");
Console.WriteLine (" {0,7:F3}", 0.006d);
Console.WriteLine (" {0,7:F3}", 0.546d);
Console.WriteLine (" {0,7:F3}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----G-----------------------Brainless L/R text alignment-------");
Console.WriteLine (" {0,6:G2}", 0.006d);
Console.WriteLine (" {0,6:G2}", 0.546d);
Console.WriteLine (" {0,6:G2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:G2}", 0.006d);
Console.WriteLine (" {0,-6:G2}", 0.546d);
Console.WriteLine (" {0,-6:G2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----C-----------------------Dot centric L/R text alignment-----");
Console.WriteLine (" {0,6:C2}", 0.006d);
Console.WriteLine (" {0,6:C2}", 0.546d);
Console.WriteLine (" {0,6:C2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:C2}", 0.006d);
Console.WriteLine (" {0,-6:C2}", 0.546d);
Console.WriteLine (" {0,-6:C2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine("-----P--------------------------P not like C :-O ---------------");
Console.WriteLine (" {0,6:P2}", 0.006d);
Console.WriteLine (" {0,6:P2}", 0.546d);
Console.WriteLine (" {0,6:P2}", 1.00d);
Console.WriteLine ("");
Console.WriteLine (" {0,-6:P2}", 0.006d);
Console.WriteLine (" {0,-6:P2}", 0.546d);
Console.WriteLine (" {0,-6:P2}", 1.00d);
Console.WriteLine ("");

Output is;

-----#--------------------------numeric value context-----------
54.60
100.00
0.30

0.006
0.546
1.000

-----F--------------------------F similar to #------------------
0.006
0.546
1.000

-----G-----------------------Brainless L/R text alignment-------
0.006
0.55
1

0.006
0.55
1

-----C-----------------------Dot centric L/R text alignment-----
$0.01
$0.55
$1.00

$0.01
$0.55
$1.00

-----P--------------------------P not like C :-O ---------------
0.60 %
54.60 %
100.00 %

0.60 %
54.60 %
100.00 %
Nov 16 '05 #4

P: n/a
shawnk <an*******@discussions.microsoft.com> wrote:

<snip>
What I would like is for the 'P' formatter to be like the 'C' formatter
in the code below. I do not want to have to use a temporary value
holder (my_original_value * 100) and the 'F' formatter since I don't
want to write additional code for the temp_value_holder.


Ah, I'm with you - sorry.

I can't seem to get it to work either, I'm afraid. The one thing you
*could* do, which is pretty foul, is to write your own IFormatProvider
which did the right thing. If you look at IFormatProvider.GetFormat
there's a pretty good example of doing something very similar.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
shawnk <an*******@discussions.microsoft.com> wrote:

<snip>
What I would like is for the 'P' formatter to be like the 'C' formatter
in the code below. I do not want to have to use a temporary value
holder (my_original_value * 100) and the 'F' formatter since I don't
want to write additional code for the temp_value_holder.


Ah, I'm with you - sorry.

I can't seem to get it to work either, I'm afraid. The one thing you
*could* do, which is pretty foul, is to write your own IFormatProvider
which did the right thing. If you look at IFormatProvider.GetFormat
there's a pretty good example of doing something very similar.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
Jon

I won't be fixing the Microsoft code (just yet :-) with a a replacement so I'l
continue to use the 'F' formatter and a *= 100 solution

Hopefull some MS people will fix the 'P' formatter so it responds to the '-' directive

Your '#' custom solution was very helpful an I'll be using it in the future

Thanks again for your excellent help

Shawn K.
Nov 16 '05 #7

P: n/a
Jon

I won't be fixing the Microsoft code (just yet :-) with a a replacement so I'l
continue to use the 'F' formatter and a *= 100 solution

Hopefull some MS people will fix the 'P' formatter so it responds to the '-' directive

Your '#' custom solution was very helpful an I'll be using it in the future

Thanks again for your excellent help

Shawn K.
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.