{"id":119,"date":"2016-07-04T23:11:44","date_gmt":"2016-07-04T22:11:44","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=119"},"modified":"2020-08-14T08:23:02","modified_gmt":"2020-08-14T07:23:02","slug":"c-nice-percentages","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-nice-percentages\/","title":{"rendered":"C# Nice percentages"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void Main()\n{\n\tvar shares = GetNicePercentagesForShares(0.01, 2, 3, 4);\n\tvar total = shares.Sum();\n\tforeach(var share in shares)\n\t\tConsole.WriteLine($\"{share}%\");\n\tConsole.WriteLine($\"total: {total}%\");\n}\n\n\/\/ 1%\n\/\/ 22%\n\/\/ 33%\n\/\/ 44%\n\/\/ total: 100%\n\npublic static List&lt;double> GetNicePercentagesForShares(params double[] shares)\n{\n    var total = shares.Sum();\n\tif (total &lt;= 0)\n\t\treturn shares.Select(i => 0.0).ToList();\n\t\t\n\tvar onePercent = 100.0 \/ total;\n    var smallEnough = 0.0000001;\n\n\tvar percentages = shares.Select(s => {\n\t\tvar percentage = Math.Round(s * onePercent);\n\t\tif (s > smallEnough &amp;&amp; percentage &lt; 1)\n            percentage = 1; \/\/ If there is a share display at least 1 percent \n\t\treturn percentage;\n\t}).ToList();\n\t\n\tvar percentTotal = percentages.Sum();\n\t\/\/ correct the largest percentage\n    if (Math.Abs(percentTotal - 100.0) > double.Epsilon) {\n\t\tint largestPercentageIndex = percentages.IndexOf(percentages.Max());\n        percentages[largestPercentageIndex] = (100.0 - percentTotal);\n\t}\t\t\n    return percentages;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":3914,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/119\/revisions\/3914"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}