- 字符串转数字
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
//字符转数字
string str1 = "2018219";
string str2 = "2018.219";//浮点数转换后的有效数为6位
int num1 = 0;
double num2 = 0.0;
stringstream s;
//转换为int类型
s << str1;
s >> num1;
//转换为double类型
s.clear();
s << str2;
s >> num2;
cout << num1 << "\n" << num2 << endl;
return 0;
}
输出为:
2018219
2018.22//有效数为6位
- 数字转字符串
#include "stdafx.h"
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string str1;
string str2 ;
int num1 = 2018219;
double num2 = 2018.219;
stringstream s;
s << num1;
s >> str1;
s.clear();
s << num2;
s >> str2;
cout << str1 << "\n" << str2 << endl;
return 0;
}
输出为:
2018219
2018.22
网友评论