阅读量:107
在C++中,使用stod函数将字符串转换为双精度浮点数时,可以通过设置std::setprecision函数来控制转换后双精度浮点数的精度。例如:
#include
#include
#include
int main() {
std::string str = "3.14159265358979323846";
double num = std::stod(str);
std::cout << std class="hljs-built_in">setprecision(10) << num class="hljs-keyword">return 0;
}
在上面的例子中,通过调用std::setprecision(10)设置了转换后双精度浮点数的精度为10。输出结果为3.1415926536。
需要注意的是,设置精度只是控制输出时的小数位数,并不会改变双精度浮点数本身的精度。