我正在开发一个程序,该程序根据用户输入的值输出花费在汽油上的总金额。我希望它输出一个小数点后 2 位的值,但程序会舍入总数,而不是输出总数应该是多少。我是初学者,不知道为什么它不能正常工作。
double gasPrice = 3.87;
double gallonsPumped = 0;
cout<<"How many gallons of gasoline (Diesel) were purchased today:"<<endl;
cin>>gallonsPumped;
int finalGasPrice = gasPrice*gallonsPumped;
cout<<endl;
if (gallonsPumped >= 1)
{
cout<<endl<<"The total cost for gasoline today was $"<<finalGasPrice<<"."<<endl;
}
else
{
cout<<"No money spent on gasoline today.";
}