{"id":7458,"date":"2026-03-28T00:01:19","date_gmt":"2026-03-28T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7458"},"modified":"2026-03-21T17:16:16","modified_gmt":"2026-03-22T00:16:16","slug":"from-decimal-to-fraction","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7458","title":{"rendered":"From Decimal to Fraction"},"content":{"rendered":"<p>I was surprised to discover that I hadn&#8217;t written about this topic before: converting a decimal value into a fraction. Of course, the solution is really stupid &mdash; which I&#8217;ll show in a moment. But the goal is to reduce or simplify the stupid  way and end up with a fraction instead of a decimal.<br \/>\n<!--more--><br \/>\nTo convert a decimal into a fraction you convert the decimal portion into an integer ratio and then reduce the fraction.<\/p>\n<p>For example, the value 0.256 can be expressed as <sup>256<\/sup>\/<sub>1000<\/sub>. This fraction is then reduced to <sup>32<\/sup>\/<sub>125<\/sub>. The value 0.25 becomes <sup>25<\/sup>\/<sub>100<\/sub> and then <sup>1<\/sup>\/<sub>4<\/sub>.<\/p>\n<p>So, the process seems rather simple: Multiply the decimal to create a larger, integer value and then reduce this fraction.<\/p>\n<p>To write the code, I borrowed from an <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5226\">Exercise<\/a> presented four years ago on reducing fractions. Here&#8217;s what I came up with:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_03_28-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_03_28-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    float decimal;\r\n    int numerator,denominator,diff,larger,smaller;\r\n\r\n    <span class=\"comments\">\/* obtain and validate input *\/<\/span>\r\n    printf(\"Enter decimal value: \");\r\n    scanf(\"%f\",&amp;decimal);\r\n    if( decimal &gt; 1.0 || decimal &lt; 0.0 )\r\n    {\r\n        puts(\"Please input a value less than 1.0\");\r\n        puts(\"and greater than zero\");\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* configure the numerator and denominator\r\n       use 100000 based on 'float' precision *\/<\/span>\r\n    denominator = 100000;\r\n    numerator = decimal*denominator;\r\n\r\n    <span class=\"comments\">\/*\r\n       Use Euclid's algorithm to find the least common\r\n       denomniator and reduce the fraction\r\n     *\/<\/span>\r\n    <span class=\"comments\">\/* calculate differences between the larger and smaller values *\/<\/span>\r\n    larger = numerator&gt;denominator ? numerator : denominator;\r\n    smaller = numerator&lt;denominator ? numerator : denominator;\r\n    diff = larger-smaller;\r\n    <span class=\"comments\">\/* keep calculating until the common denominator is found *\/<\/span>\r\n    while( diff!=larger )\r\n    {\r\n        larger = smaller&gt;diff ? smaller : diff;\r\n        smaller = smaller==larger ? diff : smaller;\r\n        diff = larger-smaller;\r\n    }\r\n\r\n    printf(\"%f is the fraction %d\/%d\\n\",\r\n            decimal,\r\n            numerator\/diff,\r\n            denominator\/diff\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The program prompts for a decimal value as input, stored in <em>float<\/em> variable <code>decimal<\/code>. A test is made to confirm that the value is in the range of 1.0 to zero &mdash; a positive decimal without an integer portion. (Though the program does allow 1.0 to be input.)<\/p>\n<p>To reduce the fraction, and employ Euclid&#8217;s algorithm, I calculate the <code>numerator<\/code> and <code>denominator<\/code> values:<\/p>\n<p><code>denominator = 100000;<br \/>\nnumerator = decimal*denominator;<\/code><\/p>\n<p>The rest of the code is lifted from the earlier Exercise&#8217;s solution. A <em>printf(<\/em>) statement outputs the results.<\/p>\n<p>Here are a few sample runs:<\/p>\n<pre>Enter decimal value: 0.875\r\n0.875000 is the fraction 7\/8\r\n\r\nEnter decimal value: 0.4\r\n0.400000 is the fraction 2\/5\r\n\r\nEnter decimal value: 0.212121\r\n0.212121 is the fraction 5303\/25000<\/pre>\n<p>Alas, the program fails to properly convert thirds:<\/p>\n<pre>Enter decimal value: 0.6666666\r\n0.666667 is the fraction 33333\/50000<\/pre>\n<p>I can read this failure in that either I&#8217;m not doing the conversion properly or there&#8217;s some other trick that I&#8217;m missing to catch certain exceptions. Or, I suppose, the problem could be with Euclid&#8217;s algorithm not being able to reduce a continuing fraction. I dunno.<\/p>\n<p>For the most part, my approach works. It solves a puzzle I hadn&#8217;t yet addressed in this blog. I&#8217;d be interested to know of any other approaches for converting decimal values into a rational representation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve covered how to get from &frac14; to 0.25 before. Now it&#8217;s time to explore how to get from 0.25 to &frac14;. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7458\">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-7458","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\/7458","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=7458"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7458\/revisions"}],"predecessor-version":[{"id":7506,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7458\/revisions\/7506"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}