C++ 将字符串转换为双精度--会丢失精度

C++ 将字符串转换为双精度--会丢失精度,c++,string,double,C++,String,Double,我无法将字符串转换为双精度字符串。我得到了一个带有lat/long坐标的字符串,格式为33.9425/N 118.4081/W 我首先调用我的函数trimLastChar(std::string&input)两次,这将删除北、南、东、西字符,然后删除正斜杠。此函数正确地将33.9425和118.4081分别作为std::string返回 我正在使用以下代码将我的std::string转换为double…但是问题是,转换会丢失精度--我怀疑它会被舍入 // Location In String i

我无法将字符串转换为双精度字符串。我得到了一个带有lat/long坐标的字符串,格式为
33.9425/N 118.4081/W

我首先调用我的函数
trimLastChar(std::string&input)
两次,这将删除北、南、东、西字符,然后删除正斜杠。此函数正确地将
33.9425
118.4081
分别作为
std::string
返回

我正在使用以下代码将我的
std::string
转换为
double
…但是问题是,转换会丢失精度--我怀疑它会被舍入

// Location In String is what trimLastChar returns
std::stringstream stream(locationInString);
std::cout << "DEBUG: before: " << locationInString << " ";
// output is a double* output = new double passed by reference to my function
stream >> output;
std::cout << output << std::endl;
正如您所注意到的,正确的值应该是
118.4081
,但缺少1


有没有办法解决这个问题?或者更重要的是,为什么会发生这种情况?

您的数字可能比输出显示的数字多。默认情况下,仅显示少量数字,您需要使用以查看更多数字。试一试

std::cout << std::setprecision(10) << output << std::endl;

std::cout输入时没有丢失精度。它的输出丢失了

#include <iostream>
using std::cout;
using std::endl;
int main()
{
  double v = 118.4081;
  cout << v << endl;
  cout.precision(10);
  cout << v << endl;
}

…您应该检查输出格式,我想这只是cout上的格式问题。
#include <iostream>
using std::cout;
using std::endl;
int main()
{
  double v = 118.4081;
  cout << v << endl;
  cout.precision(10);
  cout << v << endl;
}
$ g++ -Wall x.cpp && ./a.out
118.408
118.4081
$