C++数值转换
-
#include <iostream>
#include <string>
#include <cmath>
int main() {
// 数字转字符串
int num = 123;
std::string strNum = std::to_string(num);
std::cout << "数字转字符串: " << strNum << std::endl;
// 字符转数字
std::string strInt = "456";
int intNum = std::stoi(strInt);
std::cout << "字符转数字: " << intNum << std::endl;
// 字符转浮点数
std::string strFloat = "789.01";
float floatNum = std::stof(strFloat);
std::cout << "字符转浮点数: " << floatNum << std::endl;
// 浮点数转字符
float floatNum2 = 123.45f;
std::string strFloat2 = std::to_string(floatNum2);
std::cout << "浮点数转字符: " << strFloat2 << std::endl;
// 双精度转字符
double doubleNum = 123.456;
std::string strDouble = std::to_string(doubleNum);
std::cout << "双精度转字符: " << strDouble << std::endl;
// 字符转双精度
std::string strDouble2 = "789.101";
double doubleNum2 = std::stod(strDouble2);
std::cout << "字符转双精度: " << doubleNum2 << std::endl;
return 0;
}
-
版权声明:
作者:kukey-admin
链接:https://kuyour.top/?p=440
来源:KuKey
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
C++数值转换
-
#include <iostream>
#include <string>
#include <cmath>
int main() {
// 数字转字符串
int num = 123;
std::string strNum……
共有 0 条评论