C++数字和字符的相互转换(库函数的使用)

C++数字和字符的相互转换(库函数的使用)

1.把数字转换为字符:

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int a = 123456;
	string s = to_string(a);
	cout << "s = " <<  s << endl;

    char str1[] = "123456";
    int x = atoi(str1);
    cout << "x = " << x << endl;

    char str2[] = "12345.6789";
    double y = atof(str2);
    printf("%.4lf\n",y);
    return 0;
}

运行结果

posted @ 2022-01-01 18:55  合肥学院王星力  阅读(394)  评论(0)    收藏  举报