C++ string数值转换

数制转换

nums to string

std::to_string(type num)

string to nums

std::stoX

stod, stof, stol, soll...

实例

#include <iostream>
#include <string>
using namespace std;

int main()
{
    double d = 1.2340678;
    int i = 1001;
    float f = 1.02234;
    string s = to_string(d);
    string s1 = to_string(i);
    string s2 = to_string(f);
    cout << "nums to string " << endl;
    cout << s << " " << s1  << " "<< s2 << endl;
    
    auto dd = stod(s);
    auto ii = stoi(s1);
    auto ff = stof(s2);
    cout << "string to nums " << endl;
    cout << dd << " " << ii  << " "<< ff << endl;
    
    return 0;
}

(也可到这里自行运行

 

posted @ 2019-07-03 10:10  yocichen  阅读(179)  评论(0)    收藏  举报