Click here to Skip to main content
12,289,457 members (33,011 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.5K views
5 bookmarked
Posted

Formatting numeric results (basic formatting)

, 8 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Formatting numeric results (basic formatting)
People often struggle to use the right kind of numeric format specifiers. So here is the list of them once again (they can be found in any C# for beginners book) or on msdn.

C[n] - Currency
D[n] - Decimal
E[n] or e[n] - Exponent
F[n] - Fixed point
G[n] - General
N[n] - Number
X[n] or x[n] - Hex

n in all cases are the precision specifiers.

Examples - Compare various output when Console.WriteLine is used with an integer value.
Console.WriteLine("{0:C4}", 5);  //Output $5.0000
Console.WriteLine("{0:D4}", 5);  //Output 0005
Console.WriteLine("{0:E4}", 5);  //Output 5.0000E+000
Console.WriteLine("{0:F4}", 5);  //Output 5.0000
Console.WriteLine("{0:G4}", 5);  //Output 5
Console.WriteLine("{0:N4}", 5);  //Output 5.0000
Console.WriteLine("{0:X4}", 5);  //Output 0005
Comparision when Console.WriteLine is used with a double value.
double l = 5.00;
Console.WriteLine("{0:C4}", l);  //Output $5.0000
Console.WriteLine("{0:N4}", l);  //Output 5.000
Console.WriteLine("{0:G4}", l);  //Output 5
Console.WriteLine("{0:F4}", l);  //Output 5.000
Console.WriteLine("{0:F4}", l);  //Output 5.000
Cheers

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhinav S
Software Developer (Senior)
India India
I don't know who I am.
Do you know who I am?

https://abhinavsblogdotcom.wordpress.com/

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 nice what i looking for Pin
Jaydeep Jadav22-Nov-11 23:12
memberJaydeep Jadav22-Nov-11 23:12 
GeneralRe: Thank you Jaydeep. Pin
Abhinav S23-Nov-11 1:45
mvpAbhinav S23-Nov-11 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 8 Jan 2011
Article Copyright 2010 by Abhinav S
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid