小编Zep*_*tuz的帖子

我似乎无法使用 set precision c++ 正确获得小数位数

我目前正在研究子例程/子程序或任何你用英语称呼它们的东西,在我试图解决的这个特定作业中,我必须计算两个单词的平均长度。

\n
void Length(string const text_1,\n            string const text_2,\n            int & total_length,\n            double & mean_length)\n{\n    total_length = text_1.length() + text_2.length();\n    mean_length = static_cast<double>(total_length) / 2;\n}\n\nvoid Length_Program(int val)\n{\n    string text_1;\n    string text_2;\n    int total_length{};\n    double mean_length{};\n\n    cout << "Mata in tv\xc3\xa5 ord: ";\n    cin >> text_1 >> text_2;\n    cout << "Totall\xc3\xa4ngd: ";\n    Length(text_1, text_2, total_length, mean_length);\n    cout << total_length << endl;\n    cout << "Medell\xc3\xa4ngd: " << fixed << setprecision(1) << mean_length;\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我已将精度设置为 set precision(1),我假设它只会写一位小数,但我一直得到两位小数。\n我的例子是: abcd E\nit 应该说它是 2.5 个单词的平均值,但它说 2.51一些原因。有人可以帮助我理解我做错了什么吗? …

c++ decimal rounding iomanip

0
推荐指数
1
解决办法
46
查看次数

标签 统计

c++ ×1

decimal ×1

iomanip ×1

rounding ×1