Converting a float to a string function in c languange

hassanayoub85
hassanayoub85 used Ask the Experts™
on
Dear all,
I have a c program, and I am searching for a function like "itoa" to allow me add a float to a string.
Any idea on how can I achieve that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Daniel Van Der WerkenIndependent Consultant

Commented:
Looks like there is no standard C library for this. You need to write it yourself. However, it's a fairly standard thing to want to do and there are many ways out there.
// Converts a floating point number to 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);
    }
}

Open in new window

Here is a link:
http://www.geeksforgeeks.org/convert-floating-point-number-string/
Most Valuable Expert 2014
Top Expert 2015
Commented:
#include <stdio.h>
  char string[100];
  float f=1.0/3.0;
  sprintf(string,"%g",f);

Author

Commented:
Dan7el:
Where is the intToStr function?
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
ozo:
I dont want to use sprintf
Daniel Van Der WerkenIndependent Consultant

Commented:
It's in the article:
 // Converts a given integer x to string str[].  d is the number
 // of digits required in 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;
}

Open in new window


Reverse:

// C program for implementation of ftoa()
#include<stdio.h>
#include<math.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--;
    }
}

Open in new window

Author

Commented:
I tried it but it didnt worked!
Can you be more specific about what you want?

Are you concerned with buffer overflow?
Karrtik IyerSoftware Architect

Commented:
What are your concerns with sprintf, why you do not want to use it? Can you please specify your exact requirement?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial