Converting a float to a string function in c languange

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?
hassanayoub85Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
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/
0
ozoCommented:
#include <stdio.h>
  char string[100];
  float f=1.0/3.0;
  sprintf(string,"%g",f);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hassanayoub85Author Commented:
Dan7el:
Where is the intToStr function?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hassanayoub85Author Commented:
ozo:
I dont want to use sprintf
0
Daniel Van Der WerkenIndependent ConsultantCommented:
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

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

Are you concerned with buffer overflow?
0
Karrtik IyerSoftware ArchitectCommented:
What are your concerns with sprintf, why you do not want to use it? Can you please specify your exact requirement?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.