- 字符串轉(zhuǎn)數(shù)字
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
//字符轉(zhuǎn)數(shù)字
string str1 = "2018219";
string str2 = "2018.219";//浮點(diǎn)數(shù)轉(zhuǎn)換后的有效數(shù)為6位
int num1 = 0;
double num2 = 0.0;
stringstream s;
//轉(zhuǎn)換為int類型
s << str1;
s >> num1;
//轉(zhuǎn)換為double類型
s.clear();
s << str2;
s >> num2;
cout << num1 << "\n" << num2 << endl;
return 0;
}
輸出為:
2018219
2018.22//有效數(shù)為6位
- 數(shù)字轉(zhuǎn)字符串
#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