{"id":82,"date":"2016-10-08T20:24:00","date_gmt":"2016-10-08T20:24:00","guid":{"rendered":"http:\/\/uysalyilmaz.com\/wordpress\/?p=82"},"modified":"2016-10-08T20:25:49","modified_gmt":"2016-10-08T20:25:49","slug":"c-string-format-for-double","status":"publish","type":"post","link":"http:\/\/uysalyilmaz.com\/index.php\/2016\/10\/08\/c-string-format-for-double\/","title":{"rendered":"C# \u2013 String format for Double"},"content":{"rendered":"<pre class=\"lang:c# decode:true  \">using System;\r\n \r\npublic class Program\r\n{\r\n  public static void Main(string[] args)\r\n    {\r\n        \/\/ Pattern 1: just two decimal places\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(\"Pattern 1: just two decimal places\");\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(String.Format(\"{0:0.00}\", 123.4567));      \/\/ \"123.46\"\r\n        Console.WriteLine(String.Format(\"{0:0.00}\", 123.4));         \/\/ \"123.40\"\r\n        Console.WriteLine(String.Format(\"{0:0.00}\", 123.0));         \/\/ \"123.00\"\r\n \r\n        \/\/Pattern 2: max. two decimal places\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(\"Pattern 2: max. two decimal places\");\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(String.Format(\"{0:0.##}\", 123.4567));      \/\/ \"123.46\"\r\n        Console.WriteLine(String.Format(\"{0:0.##}\", 123.4));         \/\/ \"123.4\"\r\n        Console.WriteLine(String.Format(\"{0:0.##}\", 123.0));         \/\/ \"123\"\r\n \r\n        \/\/Pattern 3: at least two digits before decimal point\r\n        Console.WriteLine(\"***************************************************\");\r\n        Console.WriteLine(\"Pattern 3: at least two digits before decimal point\");\r\n        Console.WriteLine(\"***************************************************\");\r\n        Console.WriteLine(String.Format(\"{0:00.0}\", 123.4567));      \/\/ \"123.5\"\r\n        Console.WriteLine(String.Format(\"{0:00.0}\", 23.4567));       \/\/ \"23.5\"\r\n        Console.WriteLine(String.Format(\"{0:00.0}\", 3.4567));        \/\/ \"03.5\"\r\n        Console.WriteLine(String.Format(\"{0:00.0}\", -3.4567));       \/\/ \"-03.5\"\r\n \r\n        \/\/Pattern 4: Thousand Separators\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(\"Pattern 4: Thousand Separators\");\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(String.Format(\"{0:0,0.0}\", 12345.67));     \/\/ \"12,345.7\"\r\n        Console.WriteLine(String.Format(\"{0:0,0}\", 12345.67));       \/\/ \"12,346\"\r\n \r\n        \/\/Pattern 5: Align Number with spaces\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(\"Pattern 5: Align Number with spaces\");\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(String.Format(\"{0,10:0.0}\", 123.4567));    \/\/ \"     123.5\"\r\n        Console.WriteLine(String.Format(\"{0,-10:0.0}\", 123.4567));   \/\/ \"123.5     \"\r\n        Console.WriteLine(String.Format(\"{0,10:0.0}\", -123.4567));   \/\/ \"    -123.5\"\r\n        Console.WriteLine(String.Format(\"{0,-10:0.0}\", -123.4567));  \/\/ \"-123.5    \"\r\n \r\n       \/\/Pattern 6: Numbers between zero and one\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(\"Pattern 6: Numbers between zero and one\");\r\n        Console.WriteLine(\"**************************************\");\r\n        Console.WriteLine(String.Format(\"{0:0.0}\", 0.0));            \/\/ \"0.0\"\r\n        Console.WriteLine(String.Format(\"{0:0.#}\", 0.0));            \/\/ \"0\"\r\n        Console.WriteLine(String.Format(\"{0:#.0}\", 0.0));            \/\/ \".0\"\r\n        Console.WriteLine(String.Format(\"{0:#.#}\", 0.0));            \/\/ \"\"     \r\n \r\n      \/\/Pattern 7: Custom formatting for negative numbers and zero\r\n        Console.WriteLine(\"**********************************************************\");\r\n        Console.WriteLine(\"Pattern 7: Custom formatting for negative numbers and zero\");\r\n        Console.WriteLine(\"**********************************************************\");\r\n        Console.WriteLine(String.Format(\"{0:0.00;minus 0.00;zero}\", 123.4567));   \/\/ \"123.46\"\r\n        Console.WriteLine(String.Format(\"{0:0.00;minus 0.00;zero}\", -123.4567));  \/\/ \"minus 123.46\"\r\n        Console.WriteLine(String.Format(\"{0:0.00;minus 0.00;zero}\", 0.0));        \/\/ \"zero\"\r\n    }\r\n}<\/pre>\n<p><a href=\"http:\/\/www.csharpstar.com\/csharp-string-format-for-double\/\" target=\"_blank\">Kaynak<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System; public class Program { public static void Main(string[] args) { \/\/ Pattern 1: just two decimal places Console.WriteLine(&#8220;**************************************&#8221;); Console.WriteLine(&#8220;Pattern 1: just two decimal places&#8221;); Console.WriteLine(&#8220;**************************************&#8221;); Console.WriteLine(String.Format(&#8220;{0:0.00}&#8221;, 123.4567)); \/\/ &#8220;123.46&#8221; Console.WriteLine(String.Format(&#8220;{0:0.00}&#8221;, 123.4)); \/\/ &#8220;123.40&#8221; Console.WriteLine(String.Format(&#8220;{0:0.00}&#8221;, 123.0)); \/\/ &#8220;123.00&#8221; \/\/Pattern 2: max. two decimal places Console.WriteLine(&#8220;**************************************&#8221;); Console.WriteLine(&#8220;Pattern 2: max. two decimal places&#8221;); Console.WriteLine(&#8220;**************************************&#8221;); Console.WriteLine(String.Format(&#8220;{0:0.##}&#8221;, 123.4567)); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[32],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-asp-net-c","tag-double-format"],"_links":{"self":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":1,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":83,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions\/83"}],"wp:attachment":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}