{"id":5352,"date":"2022-05-08T00:01:06","date_gmt":"2022-05-08T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5352"},"modified":"2022-05-07T10:02:57","modified_gmt":"2022-05-07T17:02:57","slug":"splitting-a-decimal-value-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5352","title":{"rendered":"Splitting a Decimal Value &#8211; Solution"},"content":{"rendered":"<p>Cleaving a decimal value into its integer and fractional portions is a neat trick, one that relies upon C natural capability to thwack off the decimal portion of a <em>float<\/em> when it&#8217;s recast as an integer. Knowing about this conversion makes solving <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5329\">this month&#8217;s Exercise<\/a> a whole lot easier.<br \/>\n<!--more--><br \/>\nOnly two statements are added for my solution, shown below:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_05-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_05-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float input,fractional;\r\n    int integer;\r\n\r\n    printf(\"Enter a decimal value: \");\r\n    scanf(\"%f\",&amp;input);\r\n\r\n    integer = (int)input;\r\n    fractional = input - integer;\r\n    printf(\"Value %f can be split in to %d and %f\\n\",\r\n            input,integer,fractional\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The first statement provides the conversion:<\/p>\n<p><code>integer = (int)input;<\/code><\/p>\n<p>The <em>int<\/em> typecast converts <em>float<\/em> value <code>input<\/code> into an integer, effectively lopping off the decimal portion. The second statement performs the math:<\/p>\n<p><code>fractional = input - integer;<\/code><\/p>\n<p>Variable <code>fractional<\/code> is equal to the original value, <code>input<\/code>, minus its decimal portion stored in <code>integer<\/code>. The result is only the fractional value.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter a decimal value: 123.456<br \/>\nValue 123.456001 can be split in to 123 and 0.456001<\/code><\/p>\n<p>As feared, you see the <code>001<\/code> hanging off the fractional portion of the value, a remnant of the <em>float<\/em> data type&#8217;s precision. Ugh.<\/p>\n<p>I hope your solution met with success, and that you used the simple trick I did to achieve the result.<\/p>\n<p>If you&#8217;ve been hanging around this blog a while, I wrote a similar Exercise back in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1185\">February 2015<\/a>. It dealt more with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1185\">rounding functions<\/a>, though one way to round down is to use the simple typecast shown in this exercise solution.<\/p>\n<p>Alas, I don&#8217;t know of a standard library function that deals with removing the digital detritus like the 001 in 0.456001. It would be keen to have a function that limited precision to a certain range or would be smart enough to recognize a series of zeros at the end of a value and surgically tighten them up. Such a task I leave for you to explore on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cleaving a decimal value into its integer and fractional portions is a neat trick, one that relies upon C natural capability to thwack off the decimal portion of a float when it&#8217;s recast as an integer. Knowing about this conversion &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5352\">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":[5],"tags":[],"class_list":["post-5352","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5352","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=5352"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5352\/revisions"}],"predecessor-version":[{"id":5362,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5352\/revisions\/5362"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}