{"id":323,"date":"2010-07-08T20:09:05","date_gmt":"2010-07-08T20:09:05","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=323"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"display-values-in-a-string","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/strings\/display-values-in-a-string","title":{"rendered":"Display Values In A String"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Display Values With Decimal Places<br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When formatting numbers you can use &#8220;0&#8221; as mandatory place and &#8220;#&#8221; as optional place. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tString::Format(\"{0:0.####}\", 123.4567);    \/\/Gives: \"123.4667\"\n\tString::Format(\"{0:0.##}\", 123.4567);      \/\/Gives: \"123.46\"\n\tString::Format(\"{0:0.##}\", 123.4);         \/\/Gives: \"123.4\"\n\tString::Format(\"{0:0.##}\", 123.0);         \/\/Gives: \"123\"\n\n\tString::Format(\"{0:0.0#}\", 123.4567)       \/\/Gives: \"123.46\"\n\tString::Format(\"{0:0.0#}\", 123.4)          \/\/Gives: \"123.4\"\n\tString::Format(\"{0:0.0#}\", 123.0)          \/\/Gives: \"123.0\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Decimal Value<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = String::Format(\"{0:D}\", MyVariable);\n\tMyString = String::Format(\"{0:D6}\", MyVariable);\t\/\/D6 formats value to 6 digits, inserting leading zero's if necessary<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Decimal Places<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = String::Format(\"{0:F}\", MyVariable);\n\tMyString = String::Format(\"{0:F3}\", MyVariable);\t\/\/D3 formats value to 3 decimal places, inserting trailing zero's if necessary<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Hex Value<br> <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = \"0x\" + String::Format(\"{0:X2}\", MyVariable);\t\t\/\/This adds value formatted as hex with 2 digits and with 0x before it\n\n\tMyString = \"0x\" + String::Format(\"{0:X}\", 123456);\t\/\/X formats to hexadecimal\n\tMyString = \"0x\" + String::Format(\"{0:X6}\", 123456);\t\/\/<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Binary Value<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tsTemp = Convert::ToString(10, 2);\n\ttxtInputs0->Text = sTemp->PadLeft(8, '0');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Software Version Typical Solution<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = \"V:\" + Convert::ToString(FirmVer >> 8) + \".\" + Convert::ToString(String::Format(\"{0:D2}\", (FirmVer &amp; 0x00ff)));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display DateTime<br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/8kb3ddd4%28v=vs.110%29.aspx\">Link to all format codes<\/a>\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = String::Format(\"{0:dd\/MM\/yyyy HH:mm:ss}\", MyDateTime);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Good Examples<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>int neg = -10;\nint pos = 10;\n\n\/\/ C or c (Currency): It represent how many decimal place of zeros to show.\nString::Format(\"{0:C4}\", pos);      \/\/\"$10.0000\"\nString::Format(\"{0:C4}\", neg);      \/\/\"($10.0000)\"\n\n\/\/ D or d (Decimal): It represent leading zeros\nString::Format(\"{0:D4}\", pos);      \/\/\"0010\"\nString::Format(\"{0:D4}\", neg);      \/\/\"-0010\"\n\n\/\/ E or e (Exponential): It represent how many decimal places of zeros to show.\nString::Format(\"{0:E4}\", pos);      \/\/\"1.0000E+001\"\nString::Format(\"{0:E4}\", neg);      \/\/\"-1.0000E+001\"\n\n\/\/ F or f (Fixed-point): It represent how many decimal places of zeros to show.\nString::Format(\"{0:F4}\", pos);      \/\/\"10.0000\"\nString::Format(\"{0:F4}\", neg);      \/\/\"-10.0000\"\n\n\/\/ G or g (General): This does nothing\nString::Format(\"{0:G4}\", pos);      \/\/\"10\"\nString::Format(\"{0:G4}\", neg);      \/\/\"-10\"\n\n\/\/ N or n (Number): It represent how many decimal places of zeros to show.\nString::Format(\"{0:N4}\", pos);      \/\/\"10.0000\"\nString::Format(\"{0:N4}\", neg);      \/\/\"-10.0000\"\n\n\/\/ P or p (Percent): It represent how many decimal places of zeros to show.\nString::Format(\"{0:P4}\", pos);      \/\/\"1,000.0000%\"\nString::Format(\"{0:P4}\", neg);      \/\/\"-1,000.0000%\"\n\n\/\/ R or r (Round-Trip): This is invalid, FormatException is thrown.\nString::Format(\"{0:R4}\", pos);      \/\/FormatException thrown\nString::Format(\"{0:R4}\", neg);      \/\/FormatException thrown\n\n\/\/ X or x (Hex): It represent leading zeros\nString::Format(\"{0:X4}\", pos);      \/\/\"000A\"\nString::Format(\"{0:X4}\", neg);      \/\/\"FFFFFFF6\"\n\n\/\/ nothing: This is invalid, no exception is thrown.\nString::Format(\"{0:4}\", pos));      \/\/\"4\"\nString::Format(\"{0:4}\", neg));      \/\/\"-4\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/blogs.msdn.com\/b\/kathykam\/archive\/2006\/03\/29\/564426.aspx\">http:\/\/blogs.msdn.com\/b\/kathykam\/archive\/2006\/03\/29\/564426.aspx<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> Another Example<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>int main(void)\n{\n    \/\/ Format a negative integer or floating-point number in\n    \/\/ various ways.\n    Console::WriteLine(\"Standard Numeric Format Specifiers\");\n    resultString = String::Format(CultureInfo::CurrentCulture,\n        \"(C) Currency: . . . . . . . . {0:C}\\n\" +\n        \"(D) Decimal:. . . . . . . . . {0:D}\\n\" +\n        \"(E) Scientific: . . . . . . . {1:E}\\n\" +\n        \"(F) Fixed point:. . . . . . . {1:F}\\n\" +\n        \"(G) General:. . . . . . . . . {0:G}\\n\" +\n        \"    (default):. . . . . . . . {0} (default = 'G')\\n\" +\n        \"(N) Number: . . . . . . . . . {0:N}\\n\" +\n        \"(P) Percent:. . . . . . . . . {1:P}\\n\" +\n        \"(R) Round-trip: . . . . . . . {1:R}\\n\" +\n        \"(X) Hexadecimal:. . . . . . . {0:X}\\n\",\n        -123, -123.45f);\n    Console::WriteLine(resultString);\n\n    \/\/ Format the current date in various ways.\n    Console::WriteLine(\"Standard DateTime Format Specifiers\");\n    resultString = String::Format(CultureInfo::CurrentCulture,\n        \"(d) Short date: . . . . . . . {0:d}\\n\" +\n        \"(D) Long date:. . . . . . . . {0:D}\\n\" +\n        \"(t) Short time: . . . . . . . {0:t}\\n\" +\n        \"(T) Long time:. . . . . . . . {0:T}\\n\" +\n        \"(f) Full date\/short time: . . {0:f}\\n\" +\n        \"(F) Full date\/long time:. . . {0:F}\\n\" +\n        \"(g) General date\/short time:. {0:g}\\n\" +\n        \"(G) General date\/long time: . {0:G}\\n\" +\n        \"    (default):. . . . . . . . {0} (default = 'G')\\n\" +\n        \"(M) Month:. . . . . . . . . . {0:M}\\n\" +\n        \"(R) RFC1123:. . . . . . . . . {0:R}\\n\" +\n        \"(s) Sortable: . . . . . . . . {0:s}\\n\" +\n        \"(u) Universal sortable: . . . {0:u} (invariant)\\n\" +\n        \"(U) Universal full date\/time: {0:U}\\n\" +\n        \"(Y) Year: . . . . . . . . . . {0:Y}\\n\",\n        thisDate);\n    Console::WriteLine(resultString);\n\n    \/\/ Format a Color enumeration value in various ways.\n    Console::WriteLine(\"Standard Enumeration Format Specifiers\");\n    resultString = String::Format(CultureInfo::CurrentCulture,\n        \"(G) General:. . . . . . . . . {0:G}\\n\" +\n        \"    (default):. . . . . . . . {0} (default = 'G')\\n\" +\n        \"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\\n\" +\n        \"(D) Decimal number: . . . . . {0:D}\\n\" +\n        \"(X) Hexadecimal:. . . . . . . {0:X}\\n\",\n        Color::Green);\n    Console::WriteLine(resultString);\n};\n\nThis code example produces the following results:\n\nStandard Numeric Format Specifiers\n(C) Currency: . . . . . . . . ($123.00)\n(D) Decimal:. . . . . . . . . -123\n(E) Scientific: . . . . . . . -1.234500E+002\n(F) Fixed point:. . . . . . . -123.45\n(G) General:. . . . . . . . . -123\n(default):. . . . . . . . -123 (default = 'G')\n(N) Number: . . . . . . . . . -123.00\n(P) Percent:. . . . . . . . . -12,345.00 %\n(R) Round-trip: . . . . . . . -123.45\n(X) Hexadecimal:. . . . . . . FFFFFF85\n\nStandard DateTime Format Specifiers\n(d) Short date: . . . . . . . 6\/26\/2004\n(D) Long date:. . . . . . . . Saturday, June 26, 2004\n(t) Short time: . . . . . . . 8:11 PM\n(T) Long time:. . . . . . . . 8:11:04 PM\n(f) Full date\/short time: . . Saturday, June 26, 2004 8:11 PM\n(F) Full date\/long time:. . . Saturday, June 26, 2004 8:11:04 PM\n(g) General date\/short time:. 6\/26\/2004 8:11 PM\n(G) General date\/long time: . 6\/26\/2004 8:11:04 PM\n(default):. . . . . . . . 6\/26\/2004 8:11:04 PM (default = 'G')\n(M) Month:. . . . . . . . . . June 26\n(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT\n(s) Sortable: . . . . . . . . 2004-06-26T20:11:04\n(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)\n(U) Universal full date\/time: Sunday, June 27, 2004 3:11:04 AM\n(Y) Year: . . . . . . . . . . June, 2004\n\nStandard Enumeration Format Specifiers\n(G) General:. . . . . . . . . Green\n(default):. . . . . . . . Green (default = 'G')\n(F) Flags:. . . . . . . . . . Green (flags or integer)\n(D) Decimal number: . . . . . 3\n(X) Hexadecimal:. . . . . . . 00000003<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Display Values With Decimal Places When formatting numbers you can use &#8220;0&#8221; as mandatory place and &#8220;#&#8221; as optional place. Display Decimal Value Display Decimal Places Display Hex Value Display Binary Value Display Software Version Typical Solution Display DateTime Link to all format codes Good Examples http:\/\/blogs.msdn.com\/b\/kathykam\/archive\/2006\/03\/29\/564426.aspx Another Example<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":14,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":1537,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/323\/revisions\/1537"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}