当我尝试在 C++ 中使用 setprecision(2) 对数字进行舍入时,会重新返回像“0.093”这样的数字——三位,而不是小数点后两位!我不知道为什么会这样。我在下面包含了我非常基本的代码,以防我严重遗漏了一些要点。谢谢!
#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;
}