0% found this document useful (0 votes)
945 views

Convert A Floating Point Number To String in C

The document describes a C function called ftoa() that converts a floating-point number to a string. It takes the number, an array to store the string, and the number of digits after the decimal as parameters. It separates the integer and fractional parts, converts them to strings separately, and combines them into the output string. The implementation uses other functions like intToStr() that converts an integer to a string. It demonstrates converting 233.007 to the string "233.0070".

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
945 views

Convert A Floating Point Number To String in C

The document describes a C function called ftoa() that converts a floating-point number to a string. It takes the number, an array to store the string, and the number of digits after the decimal as parameters. It separates the integer and fractional parts, converts them to strings separately, and combines them into the output string. The implementation uses other functions like intToStr() that converts an integer to a string. It demonstrates converting 233.007 to the string "233.0070".

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Convert a oating point number to string in C


Write a C function ftoa() that converts a given oating-point number or a double to a string. Use of standard library functions for direct
conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string.

ftoa(n, res, afterpoint)


n --> Input Number
res[] --> Array where output string to be stored
afterpoint --> Number of digits to be considered after the point.

Example:

ftoa(1.555, str, 2) should store “1.55” in res.


ftoa(1.555, str, 0) should store “1” in res.

A simple way is to use sprintf(), but use of standard library functions for direct conversion is not allowed.

Approach: The idea is to separate integral and fractional parts and convert them to strings separately. Following are the detailed steps.

1. Extract integer part from oating-point or double number.


2. First, convert integer part to the string.
3. Extract fraction part by exacted integer part from n.
4. If d is non-zero, then do the following.
a. Convert fraction part to an integer by multiplying it with pow(10, d)
b. Convert the integer value to string and append to the result.

Following is C implementation of the above approach.

// C program for implementation of ftoa()


#include <math.h>
#include <stdio.h>
  
// Reverses a string 'str' of length 'len'
void reverse(char* str, int len)
{
    int i = 0, j = len - 1, temp;
    while (i < j) {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++;
        j--;
    }
}
  
// Converts a given integer x to string str[]. 
// d is the number of digits required in the output. 
// If d is more than the number of digits in x, 
// then 0s are added at the beginning.
int intToStr(int x, char str[], int d)
{
    int i = 0;
    while (x) {
        str[i++] = (x % 10) + '0';
        x = x / 10;
    }
  
    // If number of digits required is more, then
    // add 0s at the beginning
    while (i < d)
        str[i++] = '0';
  
    reverse(str, i);
    str[i] = '\0';
    return i;
}
  
// Converts a floating-point/double number to a string.
void ftoa(float n, char* res, int afterpoint)
{
    // Extract integer part
    int ipart = (int)n;
  
    // Extract floating part
    float fpart = n - (float)ipart;
  
    // convert integer part to string
    int i = intToStr(ipart, res, 0);
  
    // check for display option after point
    if (afterpoint != 0) {
        res[i] = '.'; // add dot
  
        // Get the value of fraction part upto given no.
        // of points after dot. The third parameter 
        // is needed to handle cases like 233.007
        fpart = fpart * pow(10, afterpoint);
  
        intToStr((int)fpart, res + i + 1, afterpoint);
    }
}
  
// Driver program to test above function
int main()
{
    char res[20];
    float n = 233.007;
    ftoa(n, res, 4);
    printf("\"%s\"\n", res);
    return 0;
}

Output:
"233.0070"

Note: The program performs similar operation if instead of oat, a double type is taken.

You might also like