{"id":34,"date":"2010-06-10T12:00:11","date_gmt":"2010-06-10T12:00:11","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=34"},"modified":"2026-06-17T09:25:03","modified_gmt":"2026-06-17T08:25:03","slug":"rounding-values","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/maths\/rounding-values","title":{"rendered":"Rounding values"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;math.h><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">rint() Round to nearest integer<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">(int) does not round values correctly!!!!!<br>Converting a value with decimal places to an int using (int) will convert ignoring the decimal places completly.  If you want a value of say 5.6 to convert to and int of 6 and not 5 then use:  (int)rint() <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double rint (double x)\nfloat rintf (float x)\nlong double rintl (long double x)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The rint() function rounds a double to the nearest integer. &nbsp;It&#8217;s output is still a double;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance if double d = 5.83<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = (WORD)d;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Would produce x=5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = (WORD)rint(d);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Would produce x=6<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Doing this in simple code without rint()<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    double my_double = 1.500;\n    my_double = round_d(my_double);\n\n\ndouble round_d(double number)\n{\n    return (double)((number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5));\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">floor() Round downwards to the nearest integer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>double floor (double x)\nfloat floorf (float x)\nlong double floorl (long double x)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">ceil() Round upwards to the nearest integer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>double ceil (double x)\nfloat ceilf (float x)\nlong double ceill (long double x)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>rint() Round to nearest integer (int) does not round values correctly!!!!!Converting a value with decimal places to an int using (int) will convert ignoring the decimal places completly. If you want a value of say 5.6 to convert to and int of 6 and not 5 then use: (int)rint() The rint() function rounds a double [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-maths"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/34\/revisions\/1119"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}