{"id":5341,"date":"2022-05-14T00:01:33","date_gmt":"2022-05-14T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5341"},"modified":"2022-05-07T10:04:43","modified_gmt":"2022-05-07T17:04:43","slug":"from-decimal-value-to-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5341","title":{"rendered":"From Decimal Value to a String"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5329\">this month&#8217;s Exercise<\/a> is to split a decimal value into its integer and fractional portions. But what if you need the fractional portion as a string?<br \/>\n<!--more--><br \/>\nIt&#8217;s uncomplicated to use math to cleave the integer and decimal portion of a value: Changing the data type of <em>float<\/em> 123.456 into an <em>int<\/em> creates 123. The rest of the operation is just math.<\/p>\n<p>Splitting the value as a string involves converting the real number into a string, then hacking off the integer portion or any text to the left of &mdash; and including &mdash; the decimal. So the string:<\/p>\n<p><code>\"123.456\"<\/code><\/p>\n<p>Becomes:<\/p>\n<p><code>\"456\"<\/code><\/p>\n<p>The function that does the heavy lifting here is <em>snprintf()<\/em>. This cousin to <em>printf()<\/em> sends its output to a buffer instead of standard output. The <em>n<\/em> in the function name indicates the buffer size, which makes this cousin better than the <em>sprintf()<\/em> function it replaces. The only caveat is that the function is non-standard, so it&#8217;s unavailable in some compilers. (Read more about <em>snprintf()<\/em> <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3921\">here<\/a>.)<\/p>\n<p>To place the decimal value <code>a<\/code> into <em>char<\/em> <code>buffer<\/code> with length <code>size<\/code>, the following statement is used:<\/p>\n<p><code>snprintf(buffer,size,\"%f\",a);<\/code><\/p>\n<p>Once the <em>float<\/em> value is converted into a string, a loop processes the characters, plucking out only those that follow the decimal. Two buffers are used for this process, as illustrated in Figure 1.<\/p>\n<div id=\"attachment_5347\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5347\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/05\/0514-fig1.png\" alt=\"\" width=\"550\" height=\"334\" class=\"size-full wp-image-5347\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/05\/0514-fig1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/05\/0514-fig1-300x182.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/05\/0514-fig1-494x300.png 494w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-5347\" class=\"wp-caption-text\">Figure 1. Copying the fractional portion into a new buffer.<\/p><\/div>\n<p>Another approach, one that my memory-stingy programmer upbringing encourages me to try, is to copy the data within the same buffer. After all, the string&#8217;s decimal portion is unnecessary, and the <code>%f<\/code> conversion character is configured by default to prefix <code>0.<\/code> to any fractional value. This approach is illustrated in Figure 2.<\/p>\n<div id=\"attachment_5348\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5348\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/05\/0514-fig2.gif\" alt=\"\" width=\"550\" height=\"137\" class=\"size-full wp-image-5348\" \/><p id=\"caption-attachment-5348\" class=\"wp-caption-text\">Figure 2. Copying the fractional portion within the same buffer.<\/p><\/div>\n<p>The following code includes the <em>decimal()<\/em> function, which returns a string representing the decimal portion of an input value. It uses the technique illustrated in Figure 2 to process the string.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_05_14-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_05_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *decimal(float a)\r\n{\r\n    const int size=32;\r\n    static char buffer[size];        <span class=\"comments\">\/* more than enough room *\/<\/span>\r\n    int offset,base;\r\n\r\n    snprintf(buffer,size,\"%f\",a);\r\n    offset = base = 0;\r\n        <span class=\"comments\">\/* first, locate the decimal *\/<\/span>\r\n    while( buffer[offset]!='.' )\r\n    {\r\n        offset++;\r\n    }\r\n        <span class=\"comments\">\/* copy the characters to the front *\/<\/span>\r\n    offset++;        <span class=\"comments\">\/* skip over the decimal *\/<\/span>\r\n    while( buffer[offset] )\r\n    {\r\n        buffer[base] = buffer[offset];\r\n        offset++;\r\n        base++;\r\n    }\r\n    buffer[base] = '\\0';    <span class=\"comments\">\/* cap the string *\/<\/span>\r\n\r\n    return(buffer);\r\n}\r\n\r\nint main()\r\n{\r\n    float a;\r\n\r\n    printf(\"Enter a decimal value: \");\r\n    scanf(\"%f\",&amp;a);\r\n\r\n    printf(\"Decimal portion is: %s\\n\",decimal(a));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>buffer<\/code> is declared as <em>static<\/em> at Line 6 to ensure that its contents aren&#8217;t discarded when the function returns. The <em>snprintf()<\/em> function at Line 9 converts <em>float<\/em> value <code>a<\/code> into a string in <code>buffer<\/code>.<\/p>\n<p>The <em>while<\/em> loop at Line 12 locates the decimal within the string, the dividing character between the value&#8217;s integer and fractional part.<\/p>\n<p>A second <em>while<\/em> loop at Line 18 moves characters from the fractional part forward.<\/p>\n<p>Line 24 caps the new string, truncating the old string, as illustrated in Figure 2.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter a decimal value: 123.456<br \/>\nDecimal portion is: 456001<\/code><\/p>\n<p>A problem occurs due to precision, as the decimal portion .456 appears as 0.456001. I suppose you could deal with the digital detritus, though doing so it&#8217;s part of this code. Effectively, the <em>decimal()<\/em> function returns the fractional part of a real number as a string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematically, it&#8217;s easy to split off the fractional part of a value. But can you do the same with a string? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5341\">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-5341","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\/5341","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=5341"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5341\/revisions"}],"predecessor-version":[{"id":5363,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5341\/revisions\/5363"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}