G
Guest
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
} ", 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
} ", 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
} ", percent_00_01 )
Console.WriteLine(" Percent 00 10 = {0, 5
} ", percent_00_10 )
Console.WriteLine(" Percent 01 00 = {0, 5
} ", percent_01_00 )
Console.WriteLine(" Percent 10 00 = {0, 5
} ", 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
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

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

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

Console.WriteLine(" Percent 00 10 = {0, 5

Console.WriteLine(" Percent 01 00 = {0, 5

Console.WriteLine(" Percent 10 00 = {0, 5

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