我使用值 a、b、c、d 来导出 e 的值。如何将 a、b、c、d 设置为小数点后四位,而将 e 设置为小数点后一位?
I use the value a, b, c, d to derive the value of e. How can I set a, b, c, d to four decimal places whereas e to one decimal place?
下面的代码是将a,b,c,d的值显示到小数点后四位,e的值显示到小数点后一位。但是,当我 运行 函数时,所有五个变量都保留一位小数。你能告诉我我的代码有什么问题吗?我该如何解决?
#include <iomanip>
#include <algorithm>
#include <vector>
struct Record{
int year;
string name;
string team;
double completions, attempts, yards, touchdowns, interceptions, e;
};
void passerRate ()
{
double a, b, c, d, e;
//quarterbacks is a vector of struct
for (int i = 0; i < quarterbacks.size(); i++){
Record player = quarterbacks[i];
a = (player.completions / player.attempts - 0.3) * 5;
b = (player.yards / player.attempts - 3) * 0.25;
c = (player.touchdowns / player.attempts) * 20;
d = 2.375 - (player.interceptions / player.attempts * 25);
cout << fixed << setprecision(4); // I want to set the precision of a,b,c,d to four decimal places
e = (a + b + c + d) / 6 * 100;
cout << fixed << setprecision(1); //I want to set the precision of e to one decimal place
quarterbacks[i].e = e;
}
您可以为 cout
.
的特定用法设置精度
// displays a to 4 decimal places
cout << setprecision(4) << a;
// displays e to 1 decimal place
cout << setprecision(1) << e;
除此之外,e = = (a + b + c + d) / 6 * 100;
没有任何意义。我假设您打算使用单个 =
.
行cout << setprecision(4) << fixed;
将仅影响十进制值显示到标准输出的方式。这里有两个解决方案应该可以帮助你。如果您想要的是在将数字用于其他计算之前对其进行四舍五入,则以下内容应该适合您:
double x = 1.23456;
double x1 = round(x); // x1 is x rounded to nearest whole number
double x2 = round(x * 10.0) / 10.0; // x2 is x rounded to nearest tenth
double x3 = round(x * 1000.0) / 1000.0; // x3 is x rounded nearest thousandth
否则,您可以在打印语句之间设置标准输出的精度,如下所示:
cout << setprecision(4) << fixed; // cout will now print 4 decimal places
cout << "a = " << a << endl;
cout << setprecision(1); // cout will now print 1 decimal place
cout << "e = " << e << endl;
下面的代码是将a,b,c,d的值显示到小数点后四位,e的值显示到小数点后一位。但是,当我 运行 函数时,所有五个变量都保留一位小数。你能告诉我我的代码有什么问题吗?我该如何解决?
#include <iomanip>
#include <algorithm>
#include <vector>
struct Record{
int year;
string name;
string team;
double completions, attempts, yards, touchdowns, interceptions, e;
};
void passerRate ()
{
double a, b, c, d, e;
//quarterbacks is a vector of struct
for (int i = 0; i < quarterbacks.size(); i++){
Record player = quarterbacks[i];
a = (player.completions / player.attempts - 0.3) * 5;
b = (player.yards / player.attempts - 3) * 0.25;
c = (player.touchdowns / player.attempts) * 20;
d = 2.375 - (player.interceptions / player.attempts * 25);
cout << fixed << setprecision(4); // I want to set the precision of a,b,c,d to four decimal places
e = (a + b + c + d) / 6 * 100;
cout << fixed << setprecision(1); //I want to set the precision of e to one decimal place
quarterbacks[i].e = e;
}
您可以为 cout
.
// displays a to 4 decimal places
cout << setprecision(4) << a;
// displays e to 1 decimal place
cout << setprecision(1) << e;
除此之外,e = = (a + b + c + d) / 6 * 100;
没有任何意义。我假设您打算使用单个 =
.
行cout << setprecision(4) << fixed;
将仅影响十进制值显示到标准输出的方式。这里有两个解决方案应该可以帮助你。如果您想要的是在将数字用于其他计算之前对其进行四舍五入,则以下内容应该适合您:
double x = 1.23456;
double x1 = round(x); // x1 is x rounded to nearest whole number
double x2 = round(x * 10.0) / 10.0; // x2 is x rounded to nearest tenth
double x3 = round(x * 1000.0) / 1000.0; // x3 is x rounded nearest thousandth
否则,您可以在打印语句之间设置标准输出的精度,如下所示:
cout << setprecision(4) << fixed; // cout will now print 4 decimal places
cout << "a = " << a << endl;
cout << setprecision(1); // cout will now print 1 decimal place
cout << "e = " << e << endl;