Wednesday 14 May 2014

string.format

String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "4/10/2012" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Tuesday, April 10, 2012" LongDate
String.Format("{0:f}", dt); // "Tuesday, April 10, 2012 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Tuesday, April 10, 2012 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "4/10/2012 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "4/10/2012 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "April 10" MonthDay
String.Format("{0:y}", dt); // "April, 2012" YearMonth
String.Format("{0:r}", dt); // "Tue, 10 Apr 2012 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2012-04-10T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2012-04-10 16:05:07Z" UniversalSortableDateTime

s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
                    "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                    "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                    "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                    "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                    "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                    "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                    "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                    "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                    "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _



int neg = -10;
int pos = 10;
// C or c (Currency): It represent how many decimal place of zeros to show.
String.Format("{0:C4}", pos);      //"$10.0000"
String.Format("{0:C4}", neg);      //"($10.0000)"

// D or d (Decimal): It represent leading zeros
String.Format("{0:D4}", pos);      //"0010"
String.Format("{0:D4}", neg);      //"-0010"

// E or e (Exponential): It represent how many decimal places of zeros to show.
String.Format("{0:E4}", pos);      //"1.0000E+001"
String.Format("{0:E4}", neg);      //"-1.0000E+001"

// F or f (Fixed-point): It represent how many decimal places of zeros to show.
String.Format("{0:F4}", pos);      //"10.0000"
String.Format("{0:F4}", neg);      //"-10.0000"

// G or g (General): This does nothing
String.Format("{0:G4}", pos);      //"10"
String.Format("{0:G4}", neg);      //"-10"

// N or n (Number): It represent how many decimal places of zeros to show.
String.Format("{0:N4}", pos);      //"10.0000"
String.Format("{0:N4}", neg);      //"-10.0000"

// P or p (Percent): It represent how many decimal places of zeros to show.
String.Format("{0:P4}", pos);      //"1,000.0000%"
String.Format("{0:P4}", neg);      //"-1,000.0000%"

// R or r (Round-Trip): This is invalid, FormatException is thrown.
String.Format("{0:R4}", pos);      //FormatException thrown
String.Format("{0:R4}", neg);      //FormatException thrown

// X or x (Hex): It represent leading zeros
String.Format("{0:X4}", pos);      //"000A"
String.Format("{0:X4}", neg);      //"FFFFFFF6"

// nothing: This is invalid, no exception is thrown.
String.Format("{0:4}", pos));      //"4"
String.Format("{0:4}", neg));      //"-4"

No comments:

Post a Comment