gpt4 book ai didi

c++ - Qt toDouble() 方法转换为 int

转载 作者:行者123 更新时间:2023-11-30 01:54:45 28 4
gpt4 key购买 nike

我在加倍转换时遇到问题。我正在将两个字符串转换为 double,一个转换得很好,另一个转换为 int,但这是相同的代码..

QTextStream lecture(&file);
ligne = lecture.readLine();
double x = ligne.split(" ")[0].toDouble();
double y = ligne.split(" ")[1].toDouble();
std::cout << " x en string = " <<ligne.split(" ")[0].toStdString() << "; y en string = " << ligne.split(" ")[1].toStdString() << std::endl;
std::cout << " x = " << x << "; y = " << y << std::endl;

这是结果

x en string = 988284.9; y en string = 6429241.49999999
x = 988285; y = 6.4496e+06

如果你看到结果,x 不是十进制..

最佳答案

x 实际上具有正确的值。

您的问题实际上在这里:

std::cout << " x = " << x ;

因为您有 6 位的默认精度,这不足以表示“.9”。它应该是这样的:

std::cout << " x = " << std::setprecision(8) << x 

哪个输出更好的值:

x = 988284.9; y = 6429241.5

注意:你需要

#include <iomanip>

参见 http://en.cppreference.com/w/cpp/io/manip/setprecision有关 setprecision()(和其他流操纵器)的附加信息

关于c++ - Qt toDouble() 方法转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21673623/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com