简体   繁体   中英

Converting string to float in C?

Right, So in my C program i have a function that takes a float value from file, and here i am trying to do the reverse, taking the string and turning it into a float.

 float PsSc = atoi(stock01[DataCount].defPsSc);

I know my error, i assumed it would work for both integers and floats, it doesn't.

I have tried

 float PsSc = atof(stock01[DataCount].defPsSc);

and that doesn't work either.

So, My question is: What can i replace my current line of code with to make it work?

Input: 1.45 . Expected output: 1.45, Real output: 1.00

Edit:

 printf("\nYour previous speed was : %.2f Metres per Second",PsSc);

The strtod() function family is what you are looking for.

Not only will strtod() convert the input string to a double (with strtof() for float and strtold() for long double ), it also tells you exactly where it stopped parsing the input string (through the second parameter).

Note that it is locale-dependent whether either strtod() or atof() expect a decimal point or a decimal comma ...

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>

#include <stdio.h>

int main()
{
    // play with "." vs. "," to see why this might be your problem
    char * input = "1.45";

    // will take a pointer beyond the last character parsed
    char * end_ptr;

    // strto...() might give an error
    errno = 0;

    // convert
    float result = strtof( input, &end_ptr );

    if ( errno == ERANGE )
    {
        // handle out-of-range error - result will be HUGE_VALF
        puts( "out of range" );
    }

    if ( end_ptr != ( input + strlen( input ) ) )
    {
        // handle incomplete parse
        printf( "Unparsed: '%s'\n", end_ptr );
    }

    printf( "result: %.2f\n", result );
    return 0;
}

Why we shouldn't use atof

On success, atof() function returns the converted floating point number as a double value. If no valid conversion could be performed, the function returns zero (0.0). If the converted value would be out of the range of representable values by a double, it causes undefined behavior .

instead we should use strtod() present in <stdlib.h>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char s[] = "1.45";
    printf("Float value : %4.2f\n",strtod(s,NULL));
    return 0;
}

it will correctly prints 1.45

See the illustration here http://ideone.com/poalgY

Try using sscanf which is much more specific:

Reference : http://www.cplusplus.com/reference/cstdio/sscanf/

float PsSc;

sscanf(stock01[DataCount].defPsSc, "%f", &PsSc);

Example : http://ideone.com/BaPmDj

#include <stdio.h>

int main(void) {
    char * str = "1.45";

    float flt;

    sscanf(str, "%f", &flt);

    printf("value = %f\n", flt);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM