From Decimal Worth to a String


The problem for this month’s Exercise is to separate a decimal worth into its integer and fractional parts. However what if you happen to want the fractional portion as a string?

It’s uncomplicated to make use of math to cleave the integer and decimal portion of a worth: Altering the info kind of float 123.456 into an int creates 123. The remainder of the operation is simply math.

Splitting the worth as a string includes changing the true quantity right into a string, then hacking off the integer portion or any textual content to the left of — and together with — the decimal. So the string:

"123.456"

Turns into:

"456"

The perform that does the heavy lifting right here is snprintf(). This cousin to printf() sends its output to a buffer as a substitute of ordinary output. The n within the perform identify signifies the buffer dimension, which makes this cousin higher than the sprintf() perform it replaces. The one caveat is that the perform is non-standard, so it’s unavailable in some compilers. (Learn extra about snprintf() here.)

To put the decimal worth a into char buffer with size dimension, the next assertion is used:

snprintf(buffer,dimension,"%f",a);

As soon as the float worth is transformed right into a string, a loop processes the characters, plucking out solely people who comply with the decimal. Two buffers are used for this course of, as illustrated in Determine 1.

Determine 1. Copying the fractional portion into a brand new buffer.

One other method, one which my memory-stingy programmer upbringing encourages me to strive, is to repeat the info inside the similar buffer. In spite of everything, the string’s decimal portion is pointless, and the %f conversion character is configured by default to prefix 0. to any fractional worth. This method is illustrated in Determine 2.

Determine 2. Copying the fractional portion inside the similar buffer.

The next code consists of the decimal() perform, which returns a string representing the decimal portion of an enter worth. It makes use of the approach illustrated in Determine 2 to course of the string.

2022_05_14-Lesson.c


#embody <stdio.h>

char *decimal(float a)
{
    const int dimension=32;
    static char buffer[size];        
    int offset,base;

    snprintf(buffer,dimension,"%f",a);
    offset = base = 0;
        
    whereas( buffer[offset]!='.' )
    {
        offset++;
    }
        
    offset++;        
    whereas( buffer[offset] )
    {
        buffer[base] = buffer[offset];
        offset++;
        base++;
    }
    buffer[base] = '';    

    return(buffer);
}

int fundamental()
{
    float a;

    printf("Enter a decimal worth: ");
    scanf("%f",&a);

    printf("Decimal portion is: %sn",decimal(a));

    return(0);
}

Variable buffer is said as static at Line 6 to make sure that its contents aren’t discarded when the perform returns. The snprintf() perform at Line 9 converts float worth a right into a string in buffer.

The whereas loop at Line 12 locates the decimal inside the string, the dividing character between the worth’s integer and fractional half.

A second whereas loop at Line 18 strikes characters from the fractional half ahead.

Line 24 caps the brand new string, truncating the outdated string, as illustrated in Determine 2.

Right here’s a pattern run:

Enter a decimal worth: 123.456
Decimal portion is: 456001

An issue happens resulting from precision, because the decimal portion .456 seems as 0.456001. I suppose you could possibly cope with the digital detritus, although doing so it’s a part of this code. Successfully, the decimal() perform returns the fractional a part of an actual quantity as a string.

Leave a Reply

Your email address will not be published.