avoid rounding error (floating specifically) c++

前端 未结 6 1544
耶瑟儿~
耶瑟儿~ 2021-02-04 12:54

http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/ I have been about this lately to review C++.

In general computing class professors tend not to cover thes

6条回答
  •  [愿得一人]
    2021-02-04 13:43

    You want to use the manipulator called "Fixed" to format your digits correctly so they do not round or show in a scientific notation after you use fixed you will also be able to use set the precision() function to set the value placement to the right of the . decimal point. the example would be as follows using your original code.

     #include 
     #include 
        int main() {
              using namespace std;
              #include 
    
    
        double dValue = 0.19213;
        cout << fixed << setprecision(2) << dValue << endl
    
    
           }
    

    outputs as:

    dValue = 0.19
    

提交回复
热议问题