How to convert string to float?

前端 未结 8 1285
执念已碎
执念已碎 2020-11-27 03:21
#include
#include

int main() 
{
    char s[100] =\"4.0800\" ; 

    printf(\"float value : %4.8f\\n\" ,(float) atoll(s)); 
    return         


        
相关标签:
8条回答
  • 2020-11-27 04:22

    Use atof()

    But this is deprecated, use this instead:

    const char* flt = "4.0800";
    float f;
    sscanf(flt, "%f", &f);
    

    http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

    atof() returns 0 for both failure and on conversion of 0.0, best to not use it.

    0 讨论(0)
  • 2020-11-27 04:23

    You want to use the atof() function.

    http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

    0 讨论(0)
提交回复
热议问题