可能重複:
Equation not working correctly in C++
Help with POW function in C++c + +浮動瓦爾四捨五入
在此代碼:
//Samuel LaManna
//Program 1 (intrest rate)
/*Variables:
Principal=P
Interest Rate=R
Times Compounded=T
Savings=S
Interest=I */
#include <iostream> //Input/output
#include <cmath> //Math Functions
using namespace std;
int main()
{
float P, R, T, S, I; //Declaring Variables
cout<<endl;
cout<<"Interest Earned Calculator"; //Prints program title
cout<<endl;
cout<<endl;
cout<<"Please enter the Principal Value: ";
cin >> P;
cout<<endl;
cout<<endl;
cout<<"Please enter the Interest Rate (in decimal form): ";
cin >> R;
cout<<endl;
cout<<endl;
cout<<"Please enter the Number of times the interest is compounded in a year: ";
cin >> T;
cout<<endl;
cout<<endl;
S=pow(1+R/T,T)*P; //Equation to find Savings
I=S-P; //Equation to find interest in $
cout<<"Interest Rate: " << R*100 <<"%" ;
cout<<endl;
cout<<endl;
cout<<"Times Compounded: " << T;
cout<<endl;
cout<<endl;
cout<<"Principal: $" << P;
cout<<endl;
cout<<endl;
cout<<"Interest: $" << I;
cout<<endl;
cout<<endl;
cout<<"Ammount in Savings: $" << S;
cout<<endl;
cout<<endl;
return 0;
}
有沒有一種方法,使最終輸出的數字輪到2位小數位即使它們是0?
[你有什麼試過?](http://mattgemmell.com/2008/12/08/what-have-you-tried) –
看看[setprecision](http://www.cplusplus。 com/reference/iostream/manipulators/setprecision /) –
我甚至不知道要開始,我是一名學生。 –