我無法將字符串轉換爲雙精度。我得到一個字符串,其格式爲: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