C++字符串数值转换
C++字符串转换为数字
int main() {
string s = "1.23456";
int intNum, floatNum, longNum, doubleNum;
intNum = stoi(s);
floatNum = stof(s);
longNum = stol(s);
return 0;
}
C字符串转换为数字
要点:
- atoi()的参数不支持string,只能是字符数组,要进行转换。
#include<string>
using namespace std;
int main() {
string s = "12.3456";
int intNum, floatNum,longNum;
intNum = atoi(s.c_str());// ascii to int numbers
floatNum = atof(s.c_str());
longNum = atol(s.c_str());
return 0;
}