我无法将字符串转换为双精度。我得到一个字符串,其格式为:33.9425/N 118.4081/W
将字符串转换为双精度 - 丢失精度
我第一次调用我的函数trimLastChar(std::string& input)
两次,它将删除North,South,East,West字符,然后是正斜杠。此功能正确地返回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;
在这种情况下,输出会产生:
33.9425
118.408
你可能注意到了,正确的值应该是118.4081
但1缺少...
任何想法如何解决?或者更重要的是,为什么发生这种情况?
...你应该检查你的输出格式,我猜这只是cout上格式化的问题。 – IdeaHat