C++ 将字符串 '024.9' 转换为 double 24.9
要将字符串 '024.9' 转换为 double 类型的 24.9,可以使用以下 C++ 代码:
#include <iostream>
#include <sstream>
int main() {
std::string str = '024.9';
double num;
// 使用 std::stringstream 进行转换
std::stringstream ss(str);
ss >> num;
std::cout << num << std::endl;
return 0;
}
输出结果为:
24.9
这里使用了 std::stringstream 来进行字符串到 double 的转换。先将字符串传入 stringstream 对象中,然后使用 ' >> ' 运算符将其转换为 double 类型的 num 变量。最后输出 num 的值即可。
原文地址: https://www.cveoy.top/t/topic/qqE1 著作权归作者所有。请勿转载和采集!