472,790 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Formatting right justified percentages with Console.writeline()

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
7 6281
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Julian Nicholls | last post by:
Hello Everybody Am I missing something, or is there no way to format numbers right-justified using Console.WriteLine(), i.e. 23 223 1023 I can get zero fill on the left, but not space...
4
by: John Sutor | last post by:
I need some code that, on each keyup event, will take all of the numbers typed into the text box and format as they type to look like this 100 1,000 10,000 100,000 1,000,000 John S
2
by: Homer Simpson | last post by:
Hi, I'm looking for a way to format the output of my double value. For example, the result of a calculation may be 12.3456789 but I want to display 12.346 (three decimal places of precision)....
2
by: djc | last post by:
before I dig into the visual studio docs looking for info on this I decided to post here to hopefully get pointed in the right direction and maybe save some some. I am playing around with...
3
by: lotus | last post by:
Hi all. I want to control two different instrtument but have simliar functionality. Acually the fisrt one is controlled by using serial communication, and the other is controlled by LAN...
5
by: MrAsm | last post by:
Hi, what is (if exist) the format string to print an integer in C# with leading spaces (to do right justification) e.g. value = 77 |<----->| <--- width = 5 characters + 12345 + | ...
0
by: EricBlair | last post by:
Hello, I wrote a windows service that is supposed to start an interactive GUI app. I realize a service will not readily do this so I've pieced together the code below to bypass that. However, the...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
13
by: cj2 | last post by:
pages = 1 pages.tostring gives me "1" pages+1.tostring gives me "2.0" Why did it add the ".0" on there? I was looking for "2" and this screws me up. Actually this is what I was doing. In...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.