{"id":289,"date":"2017-10-11T20:26:12","date_gmt":"2017-10-11T20:26:12","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=289"},"modified":"2022-08-25T16:38:21","modified_gmt":"2022-08-25T15:38:21","slug":"using-strings-values","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/strings\/using-strings-values","title":{"rendered":"Using Strings-Values"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Verifying numeric values<\/h4>\n\n\n\n<p>See <a href=\"https:\/\/ibex.tech\/csharp\/strings\/verify-numeric-value\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ToString()<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyTextBox.Text = MyVariable.ToString(\"0.0\");    \/\/Force to 1 decimal place\n\tMyTextBox.Text = MyVariable.ToString(\"0.0##\");    \/\/Force to min 1 decimal place, max 3 decimal places (123 will be \"123.0\", 12.3456 will be \"12.346\" (rounds up last digit if necessary) )\n\tMyTextBox.Text = MyVariable.ToString(\"0.###\");    \/\/Force to min 0 decimal places, max 3 decimal places (123 will be \"123\", 12.3456 will be \"12.346\" (rounds up last digit if necessary) )\n\tMyTextBox.Text = MyVariable.ToString(\"#.0##\");    \/\/Force to min 1 decimal place, max 3 decimal places, don't display leading 0 if value is 0.#\n\tMyTextBox.Text = MyVariable.ToString(\"0\");      \/\/Force to 0 decimal place\n\tMyTextBox.Text = MyVariable.ToString(\"0000\");      \/\/Force to 4 digits, inserting leading zero's if necessary\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/custom-numeric-format-strings\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/custom-numeric-format-strings<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Display Values With Decimal Places<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Fixed number of decimal places <br><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyTextBox.Text = dMyValue.ToString(\"0.000\", System.Globalization.CultureInfo.InvariantCulture);\t\/\/Display to 3 decimal places (1.23 will be displaed as \"1.230\")<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Optional number of decimal places<\/h5>\n\n\n\n<p>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);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Insert leading zero&#8217;s if necessary<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\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\/\/F3 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 &gt;&gt; 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>\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:yyyy-MM-dd HH:mm:ss}\", MyDateTime);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Verifying numeric values See here. ToString() https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/custom-numeric-format-strings Display Values With Decimal Places Fixed number of decimal places Optional number of decimal places When formatting numbers you can use &#8220;0&#8221; as mandatory place and &#8220;#&#8221; as optional place. Display Decimal Value Insert leading zero&#8217;s if necessary Display Decimal Places Display Hex Value Display Binary Value Display [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/289\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}