{"id":5381,"date":"2015-10-26T23:02:15","date_gmt":"2015-10-26T15:02:15","guid":{"rendered":"http:\/\/rmohan.com\/?p=5381"},"modified":"2015-10-26T23:02:15","modified_gmt":"2015-10-26T15:02:15","slug":"calculating-percentages-in-bash","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5381","title":{"rendered":"Calculating percentages in bash"},"content":{"rendered":"<h3 class=\"post-title entry-title\">Calculating percentages in bash<\/h3>\n<div class=\"post-header\"><\/div>\n<div id=\"post-body-2195137608663203570\" class=\"post-body entry-content\">Dividing in bash will cause problems if the result is below zero. This is a problem when you\u2019re trying to work out percentages. For example, if you simply want to divide 1 by 2 the result should be 0.5. However, bash returns the result 0:<\/p>\n<blockquote class=\"tr_bq\"><p>user@computer:~&gt; echo $(( 1 \/ 2 ))<br \/>\n0<\/p><\/blockquote>\n<p>To fix this problem use the program bc, \u201can arbitrary precision calculator language\u201d. Using bc you can do the calculation to a specified number of decimal places:<\/p>\n<blockquote class=\"tr_bq\"><p>user@computer:~&gt; echo \u201cscale=2; 1 \/ 2? | bc<br \/>\n.50<\/p><\/blockquote>\n<p>You can use this to work out the percentage as follows:<\/p>\n<blockquote class=\"tr_bq\"><p>user@computer:~&gt; echo \u201cscale=2; 1 \/ 2 * 100? | bc<br \/>\n50.00<\/p><\/blockquote>\n<p>To chop off the decimal places use sed:<\/p>\n<blockquote class=\"tr_bq\"><p>user@computer:~&gt; echo \u201cscale=2; 1 \/ 2 * 100? | \u00a0bc \u00a0| \u00a0sed s\/\\\\.[0-9]\\\\+\/\/<br \/>\n50<\/p><\/blockquote>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Calculating percentages in bash Dividing in bash will cause problems if the result is below zero. This is a problem when you\u2019re trying to work out percentages. For example, if you simply want to divide 1 by 2 the result should be 0.5. However, bash returns the result 0:<\/p>\n<p>user@computer:~&gt; echo $(( 1 \/ 2 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5381"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5381"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5381\/revisions"}],"predecessor-version":[{"id":5382,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5381\/revisions\/5382"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}