Display Values With Decimal Places

When formatting numbers you can use “0” as mandatory place and “#” as optional place.

	String::Format("{0:0.####}", 123.4567);    //Gives: "123.4667"
	String::Format("{0:0.##}", 123.4567);      //Gives: "123.46"
	String::Format("{0:0.##}", 123.4);         //Gives: "123.4"
	String::Format("{0:0.##}", 123.0);         //Gives: "123"

	String::Format("{0:0.0#}", 123.4567)       //Gives: "123.46"
	String::Format("{0:0.0#}", 123.4)          //Gives: "123.4"
	String::Format("{0:0.0#}", 123.0)          //Gives: "123.0"

Display Decimal Value

	MyString = String::Format("{0:D}", MyVariable);
	MyString = String::Format("{0:D6}", MyVariable);	//D6 formats value to 6 digits, inserting leading zero's if necessary

Display Decimal Places

	MyString = String::Format("{0:F}", MyVariable);
	MyString = String::Format("{0:F3}", MyVariable);	//D3 formats value to 3 decimal places, inserting trailing zero's if necessary

Display Hex Value

	MyString = "0x" + String::Format("{0:X2}", MyVariable);		//This adds value formatted as hex with 2 digits and with 0x before it

	MyString = "0x" + String::Format("{0:X}", 123456);	//X formats to hexadecimal
	MyString = "0x" + String::Format("{0:X6}", 123456);	//

Display Binary Value

	sTemp = Convert::ToString(10, 2);
	txtInputs0->Text = sTemp->PadLeft(8, '0');

Display Software Version Typical Solution

	MyString = "V:" + Convert::ToString(FirmVer >> 8) + "." + Convert::ToString(String::Format("{0:D2}", (FirmVer & 0x00ff)));

Display DateTime

Link to all format codes

	MyString = String::Format("{0:dd/MM/yyyy HH:mm:ss}", MyDateTime);

Good Examples

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"

http://blogs.msdn.com/b/kathykam/archive/2006/03/29/564426.aspx

Another Example

int main(void)
{
    // Format a negative integer or floating-point number in
    // various ways.
    Console::WriteLine("Standard Numeric Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(C) Currency: . . . . . . . . {0:C}\n" +
        "(D) Decimal:. . . . . . . . . {0:D}\n" +
        "(E) Scientific: . . . . . . . {1:E}\n" +
        "(F) Fixed point:. . . . . . . {1:F}\n" +
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(N) Number: . . . . . . . . . {0:N}\n" +
        "(P) Percent:. . . . . . . . . {1:P}\n" +
        "(R) Round-trip: . . . . . . . {1:R}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n",
        -123, -123.45f);
    Console::WriteLine(resultString);

    // Format the current date in various ways.
    Console::WriteLine("Standard DateTime Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(d) Short date: . . . . . . . {0:d}\n" +
        "(D) Long date:. . . . . . . . {0:D}\n" +
        "(t) Short time: . . . . . . . {0:t}\n" +
        "(T) Long time:. . . . . . . . {0:T}\n" +
        "(f) Full date/short time: . . {0:f}\n" +
        "(F) Full date/long time:. . . {0:F}\n" +
        "(g) General date/short time:. {0:g}\n" +
        "(G) General date/long time: . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(M) Month:. . . . . . . . . . {0:M}\n" +
        "(R) RFC1123:. . . . . . . . . {0:R}\n" +
        "(s) Sortable: . . . . . . . . {0:s}\n" +
        "(u) Universal sortable: . . . {0:u} (invariant)\n" +
        "(U) Universal full date/time: {0:U}\n" +
        "(Y) Year: . . . . . . . . . . {0:Y}\n",
        thisDate);
    Console::WriteLine(resultString);

    // Format a Color enumeration value in various ways.
    Console::WriteLine("Standard Enumeration Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
        "(D) Decimal number: . . . . . {0:D}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n",
        Color::Green);
    Console::WriteLine(resultString);
};

This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.