{"id":5329,"date":"2022-05-01T00:01:08","date_gmt":"2022-05-01T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5329"},"modified":"2024-03-27T16:17:22","modified_gmt":"2024-03-27T23:17:22","slug":"splitting-a-decimal-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5329","title":{"rendered":"Splitting a Decimal Value"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>A floating point, or real number, has two parts: Before the decimal comes the integer part. After the decimal is the fractional part. Sometimes it&#8217;s necessary to split such a value, so that your code can deal separately with the integer and fractional parts.<br \/>\n<!--more--><br \/>\nNever mind the reason, though it may be coming up in a future Exercise on this blog (hint-hint). Your task for this month&#8217;s Exercise is to prompt the user for floating-point input. Hopefully they type a real number, something like 123.456.<\/p>\n<p>In your code, you cleave the value, setting the integer part &mdash; 123 in this example &mdash; into its own <em>int<\/em> variable, and putting the fractional part &mdash; 456 &mdash; into its own <em>float<\/em> variable. All three values are then output: original, integer, and fractional.<\/p>\n<p>To be kind, I&#8217;m providing the following code skeleton to help you get started.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_05_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_05_01-Lesson.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    <span class=\"comments\">\/* magic happens here *\/<\/span>\r\n\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>Just replace the comment in the skeleton with your coding brilliance. Ta-da! You&#8217;re done.<\/p>\n<p>Here&#8217;s a sample run of my solution:<\/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>Remember that floating point values are imprecise, which explains how the fractional portion in the output may not be exact. Still, the point is to separate the values as shown above.<\/p>\n<p>Please try this exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5352\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use your programming prowess to cleave a decimal value in twain. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5329\">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":[3],"tags":[],"class_list":["post-5329","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5329","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=5329"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5329\/revisions"}],"predecessor-version":[{"id":6335,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5329\/revisions\/6335"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}