{"id":526,"date":"2003-10-21T22:42:48","date_gmt":"2003-10-22T02:42:48","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=526"},"modified":"2003-10-21T22:42:48","modified_gmt":"2003-10-22T02:42:48","slug":"custom-string-formatting-in-c","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/10\/21\/custom-string-formatting-in-c\/","title":{"rendered":"Custom string formatting in C#"},"content":{"rendered":"<p>Formatting strings for output into various mediums is always a fun&#8230; err.. required task.  Every language does it differently.  C# overloads the ToString() method to format a string using this syntax:<br \/>\n<code><br \/>\nConsole.WriteLine(MyDouble.ToString(\"C\"));<br \/>\n<\/code><br \/>\nwhere &#8220;C&#8221; is a format specifier specifically for locale specific currency formatting.  If the variable &#8216;MyDouble&#8217; was 3456 in the example above, you&#8217;d see:<br \/>\n<code><br \/>\n$3546.00<br \/>\n<\/code><br \/>\nprinted out.  Of course, the fun doesn&#8217;t end there.  There are a whole boatload of <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpguide\/html\/cpconstandardnumericformatstrings.asp\">standard numeric formatting specifiers<\/a> you can use including decimal, number, percent and hexadecimal.  But truly the most fun are the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpguide\/html\/cpconcustomnumericformatstrings.asp\">custom numeric format strings<\/a>.  Example: Let&#8217;s say that your boss wants you to format all product pricing rounded to the nearest dollar without using a commas (ie: $1224 instead of $1,224.00).  Normally, you&#8217;d write:<br \/>\n<code><br \/>\nPrice: &lt;%= Product.Price.ToString(\"C\") %&gt;<br \/>\n<\/code><br \/>\nbut since you don&#8217;t want to have commas, you can use a custom format string:<br \/>\n<code><br \/>\nPrice: &lt;%= Product.Price.ToString(\"$#####\") %&gt;<br \/>\n<\/code><br \/>\nwhich will produce this:<\/p>\n<p>Price: $1224<\/p>\n<p>How about phone numbers? Don&#8217;t they just suck to format? In ColdFusion, you&#8217;d have something like this:<br \/>\n<code><br \/>\n(#left(str, 3)#) #mid(str, 4, 3)# - #right(str, 4)#<br \/>\n<\/code><br \/>\nwhere &#8216;str&#8217; is a string containing the 10 digit phone number.  In C#, you can write this:<br \/>\n<code><br \/>\nphone.ToString(\"(###) ### - ####\");<br \/>\n<\/code><br \/>\nPretty concise isn&#8217;t it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Formatting strings for output into various mediums is always a fun&#8230; err.. required task. Every language does it differently. C# overloads the ToString() method to format a string using this syntax: Console.WriteLine(MyDouble.ToString(&#8220;C&#8221;)); where &#8220;C&#8221; is a format specifier specifically for locale specific currency formatting. If the variable &#8216;MyDouble&#8217; was 3456 in the example above, you&#8217;d &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/10\/21\/custom-string-formatting-in-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom string formatting in C#<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/526"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}