• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

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?
0
hassanayoub85
Asked:
hassanayoub85
1 Solution
 
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
 
hassanayoub85Author Commented:
Dan7el:
Where is the intToStr function?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now