字符串转变成数字

时间:2014-05-08 19:45:22   收藏:0   阅读:246

C标准库了提供了 atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long  long 型。

 

  char    str[] = "15.455";

 

  double     db;

 

  int     i;

 

  db = atof(str);   // db = 15.455

 

  i = atoi(str);    // i = 15

 

  若字符串为string类型,则要用c_str()方法获取其字符串指针,如下:

 

  string    str = "15.455";

 

  double     db;

 

  int     i;

 

  db = atof(str.c_str());   // db = 15.455

 

  i = atoi(str.c_str());    // i = 15

字符串转变成数字,布布扣,bubuko.com

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!