473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6327
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.