[英]C++ stod rounding the doubles
在 c++ 中,std::stod( string ) 將字符串轉換為浮點數。 所以,這樣做
std::cout << std::stod("378760.58") << "\n";
我希望它將378760.58
打印到標准輸出,但它四舍五入並打印378761
。 std::stod() 有時會按預期工作,但有時會以意想不到的方式表現得很奇怪。
誰能解釋為什么以及我可以使用什么替代方法在 c++ 中將字符串轉換為雙精度? 感謝所有回復:)
您可以添加“iomanip”頭文件並使用以下代碼:
std::cout << std::fixed << std::setprecision(2) << std::stod("378760.58") << "\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.