C++ precision of numbers and truncation with fstream

后端 未结 2 654
南笙
南笙 2021-01-20 02:40

I have a file.txt with hundreds of numbers. They have many digits (max 20) after the point and I need to get them all without truncation, otherwise they introduce errors in

2条回答
  •  佛祖请我去吃肉
    2021-01-20 03:29

    The following works fine on my system (Win7, VS2012):

    #include 
    #include 
    
    int main (void)
    {
        std::ifstream file ("test.txt") ;
    
        long double d = 0 ;
        file >> d ;
    
        std::cout.precision (20) ;
        std::cout << d << "\n" ;
    
    
        return 0 ;
    }
    

    The text file:

    2.7239385667867091

    The output:

    2.7239385667867091

    If this doesn't work on your system, then you need to use a third-party number library.

提交回复
热议问题