Is there any way in C++ (or boost lib) to show a given number digits of fraction part? But I don't want to print the trailing 0 in fraction part (eg. 1.000
, 1.500
). See this case:
cout << std::setprecision(3) << 5.0/7.0 << endl; // 0.714
cout << std::setprecision(3) << 12.0/7.0 << endl; // 1.71
cout << std::setprecision(3) << 7.0/7.0 << endl; // 1
cout << std::setprecision(3) << 10.5/7.0 << endl; // 1.5
The problem is setprecision
prints line 1 and line 2 differently, where I want to have both lines print 0.714
and 1.714
. And still keep line 3 and line 4 1
and 1.5
.