Tuesday, June 23, 2009

String.Format – How to format string in asp.net c#

String.Format – a useful reference


The String.Format routine can be very powerful – especially with numerics & padding. Here are some examples which I find a handy reference.
int neg = -10;int pos = 10;
// C or c (Currency)String.Format(”{0:C4}”, pos); //”$10.0000″String.Format(”{0:C4}”, neg); //”($10.0000)”
// D or d (Decimal) – leading zerosString.Format(”{0:D4}”, pos); //”0010″String.Format(”{0:D4}”, neg); //”-0010″
// E or e (Exponential)String.Format(”{0:E4}”, pos); //”1.0000E+001″String.Format(”{0:E4}”, neg); //”-1.0000E+001″
// F or f (Fixed-point)String.Format(”{0:F4}”, pos); //”10.0000″String.Format(”{0:F4}”, neg); //”-10.0000″
// P or p (Percent)String.Format(”{0:P4}”, pos); //”1,000.0000%”String.Format(”{0:P4}”, neg); //”-1,000.0000%”

Reference:
http://blog.stevex.net/index.php/string-formatting-in-csharp/
http://cgaskell.wordpress.com/category/aspnet-c/

No comments: