2015-06-21 94 views
-3

的四舍五入我尝试这样做:如何避免使用strtold

#include <cstdlib> 
#include <iostream> 

int main() 
{ 
    char *p; 
    long double l = strtold("12312.12345", &p); 
    std::cout << l << std::endl; //prints 12312.1 
} 

demo

如何避免那种取整的?

+0

这不是'strtold'的错。请参阅http://stackoverflow.com/questions/3923202/set-the-digits-after-decimal-point – deviantfan

回答

1

您的字符串被正确解析。你可以这样验证:

// Remove the integer part ... 
std::cout << (l - 12312) << std::endl; 
// ... and you get the fractional part correctly displayed: 
// 0.12345 

所以报告的问题只是输出格式的问题。