{"id":3082,"date":"2020-07-22T14:51:41","date_gmt":"2020-07-22T13:51:41","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3082"},"modified":"2024-05-18T12:03:57","modified_gmt":"2024-05-18T11:03:57","slug":"rounding-values","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/maths\/rounding-values","title":{"rendered":"Rounding values"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Rounding value correctly<\/h4>\n\n\n\n<p><strong><em>intval() does not round values correctly!!!!<\/em><\/strong><br>As for C++, it simply converts to int and ignores decimal places.  If you want a source value of say 5.6 to be converted to an integer of 6 and not 5, then use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyIntValue = intval(round(5.6));    \/\/&lt;&lt;&lt; intval() because round() outputs a float<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Round down<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyVariable = floor(56.78);    \/\/Returns 56<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Round Up<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyVariable = ceil(56.78);    \/\/Returns 57<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Round to a specific number of decimal places<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>echo round(12.3456789, 3);    \/\/Will print: 12.346<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Value output vs  string output<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Force to 2 decimal places (outputs as a string, so \"6.30\")\n  $MyString = number_format(\"6.3\",2);  \/\/Will display 6.30\n\n\/\/Force to 2 decimal places (outputs as a number, so 6.30)\n  $MyValue = round(\"6.3456\",2);  \/\/Will output 6.30<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rounding value correctly intval() does not round values correctly!!!!As for C++, it simply converts to int and ignores decimal places. If you want a source value of say 5.6 to be converted to an integer of 6 and not 5, then use: Round down Round Up Round to a specific number of decimal places Value [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[261],"tags":[],"class_list":["post-3082","post","type-post","status-publish","format-standard","hentry","category-maths"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3082"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3082\/revisions"}],"predecessor-version":[{"id":4630,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3082\/revisions\/4630"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}