C에서 setprecision (2)을 사용하여 숫자를 시도 할 때 "0.093"과 같은 숫자가 반환됩니다 .- 소수점 이하 두 자리가 아닌! 나는 이것이 왜 있는지를 알 수 없다. 나는 약간의 요점이 심각하게 누락 된 경우에 대비하여 나의 초보적인 코드를 아래에 포함시켰다. 감사!집합 정밀도가 제곱 된 숫자 앞에 0을줍니다.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double tax = 0.06 ; //Tax Rate
float cost ; //Cost of item
float computed ; //Non-rounded tax
cout << "Enter the cost of the item: " ;
cin >> cost ;
computed = tax*cost;
cout << "Computed: $" << computed << endl;
cout << "Charged: $" << setprecision(2) << computed << endl; //Computed tax rounded to 2 decimal places
return 0;
}
http://www.cplusplus.com/reference/iomanip/setprecision에 따르면 소수점 이하 2 자리가 아닌 _precision_ (유효 숫자 2 자리) 2 자리를 묻습니다. 아마 당신은 http://www.cplusplus.com/reference/iomanip을 봐야 할 것입니다. – vonbrand