Converting a float to a string function in c languange

Posted on 2014-08-22
Last Modified: 2015-05-28
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?
Question by:hassanayoub85
    LVL 19

    Expert Comment

    by:Daniel Van Der Werken
    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:
    LVL 84

    Accepted Solution

    #include <stdio.h>
      char string[100];
      float f=1.0/3.0;

    Author Comment

    Where is the intToStr function?

    Author Comment

    I dont want to use sprintf
    LVL 19

    Expert Comment

    by:Daniel Van Der Werken
    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


    // C program for implementation of ftoa()
    // 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 Comment

    I tried it but it didnt worked!
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    Can you be more specific about what you want?

    Are you concerned with buffer overflow?
    LVL 15

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now