string to float conversion?

前端 未结 9 1850
-上瘾入骨i
-上瘾入骨i 2020-12-18 14:16

I\'m wondering what sort of algorithm could be used to take something like \"4.72\" into a float data type, equal to

float x = 4.72;
9条回答
  •  猫巷女王i
    2020-12-18 14:50

    For C++ you can use boost::lexical_cast:

       std::string str( "4.72" );
       float x = boost::lexical_cast< float >( str );
    

    For C you can use sscanf:

       char str[]= "4.72";
       float x;
       sscanf( str, "%f", &x );
    

提交回复
热议问题