{"id":102,"date":"2013-06-08T00:01:37","date_gmt":"2013-06-08T08:01:37","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=102"},"modified":"2013-06-10T06:01:36","modified_gmt":"2013-06-10T14:01:36","slug":"round-me-up-round-me-down","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=102","title":{"rendered":"Round Me Up, Round Me Down"},"content":{"rendered":"<p>The C language library features a few rounding functions, but most of them deal with trimming floating point values down (or up) to integers. The other day I encountered an interesting problem: How to round a value up or down to the nearest value of 5.<br \/>\n<!--more--><br \/>\nBeing a geek, I figured the formula would somehow involve the modulus operator, <code>%<\/code>, which naturally lops off values. But rather than write a simple equation, I decided to devise a function. Further, I decided to craft that function so it could round up or down to any integer digit. The result is the <em>roundit()<\/em> function, illustrated in this code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint roundit(int value, int limit)\r\n{\r\n    if( limit &lt; 0)           \/* round down *\/\r\n    {\r\n        limit = abs(limit);     \/* make limit positive *\/\r\n        return(value - (value%limit));\r\n    }\r\n    else                        \/* round up *\/\r\n    {\r\n        return(value + limit - (value%limit));\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    puts(\"+5\\t+3\\tval\\t-3\\t-5\");\r\n    for(x=0;x&lt;20;x++)\r\n    {\r\n        printf(\"%2d\\t%2d\\t%2d\\t%2d\\t%2d\\n\",\r\n            roundit(x,5),\r\n            roundit(x,3),\r\n            x,\r\n            roundit(x,-3),\r\n            roundit(x,-5)\r\n            );\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>The key to making the <em>roundit()<\/em> function work is the <code>value%limit<\/code> operation. The modulus operator returns a remainder value, which can then be added or subtracted to the original value yield the rounding result desired.<\/p>\n<p>When the value is to be rounded down, the <code>limit<\/code> variable is negative. The <code>if<\/code> statement catches that condition as the first line in the <em>roundit()<\/em> function. The value of <code>limit<\/code> is then made positive, thanks to the <em>abs()<\/em>, absolute value, function. The <code>return<\/code> statement then takes the modulo value and subtracts it from the <code>value<\/code> sent. That operation rounds <code>value<\/code> down.<\/p>\n<p>To round <code>value<\/code> up, the modulo is calculated and then subtracted from the <code>limit<\/code> value. That way, the code <em>adds<\/em> the result to the value passed, rounding it up.<\/p>\n<p>The code&#8217;s output demonstrates how a value can be rounded up or down by 5 or 3. The original value is in the center column:<\/p>\n<pre><code>+5\t+3\tval\t-3\t-5\r\n 5\t 3\t 0\t 0\t 0\r\n 5\t 3\t 1\t 0\t 0\r\n 5\t 3\t 2\t 0\t 0\r\n 5\t 6\t 3\t 3\t 0\r\n 5\t 6\t 4\t 3\t 0\r\n10\t 6\t 5\t 3\t 5\r\n10\t 9\t 6\t 6\t 5\r\n10\t 9\t 7\t 6\t 5\r\n10\t 9\t 8\t 6\t 5\r\n10\t12\t 9\t 9\t 5\r\n15\t12\t10\t 9\t10\r\n15\t12\t11\t 9\t10\r\n15\t15\t12\t12\t10\r\n15\t15\t13\t12\t10\r\n15\t15\t14\t12\t10\r\n20\t18\t15\t15\t15\r\n20\t18\t16\t15\t15\r\n20\t18\t17\t15\t15\r\n20\t21\t18\t18\t15\r\n20\t21\t19\t18\t15<\/code><\/pre>\n<p>So to round the value 11 up by 5, you get 15. To round 11 down to the nearest value of 5, you get 10. That&#8217;s the way the <em>roundit()<\/em> function works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to round a value to more than just the nearest integer. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=102\">Continue reading <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":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}