C ++ program does not behave as expected, double converts when it should not be

I did a program that rounds numbers with different decimals, since the 2001.3666 example ends as 2001.37, I managed to do this job by adding 0.005 and then 100 and converting to int and then dividing by 100 again.

Everything worked fine, there were no problems there, it was fun creating some loops and stuff, but then everything became strange.

you see the first line of the program, which repeats what the user entered, began to display a rounded shape instead of the actual number entered by the user.

After some time, I came to the conclusion that this is not my code, because I started a new project and quickly compiled this code:
#include <iostream>
#include <string> 
using namespace std;


int main()
{
  cout << "enter: ";
  double numberWithDecimalPlaces;
  cin >> numberWithDecimalPlaces;
  cout << "you entered " << numberWithDecimalPlaces << endl;

  system("pause");
  return 0; 
}

2001.3666, , 2001.37

:

enter: 2001.3666
you entered 2001.37

- , , , cin → , , - - .

.

visual studio 2010 win7

+3
2

cout ++ 6 , 2001.3666 2001.37, 201.3666 201.367.

:

#include <iomanip>
...
cout << "you entered " << setprecision(10) << numberWithDecimalPlaces << endl;
+7

Source: https://habr.com/ru/post/1759128/


All Articles