我无法使设置的精度在C++
I can't get the set precision to work in C++
我无法在C 中工作。我不断获得3个小数的位置,而不是1。如果我7和32英寸,Shootp的出现为21.875。我需要它以21.9出现。我对此很陌生,我可以使用任何帮助!谢谢
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int scored, attmp;
cout<<"Enter the number of goals that were scored: ";
cin>> scored;
if( scored < 0 )
{
cout<<"Error: The number of goals must be greater than 0. Try Again:
";
cin>> scored;
}
if( scored > 0 )
{
}
cout<<"nEnter the number of shots that were attempted: ";
cin>>attmp;
if( attmp < 0 )
{
cout<<"Error: The number of goal must be greater that 0. Try again:
";
cin>> attmp;
}
if( attmp >0 )
{
}
double shootp= ((double)scored) / (attmp) * 100.0;
cout<<"nThe Shooting Percentage is "<< shootp << setprecision(1) <<
endl;
return 0;
}
您需要同时使用std ::固定操纵器和std :: setPrecision对象,然后再输出浮点变量:
std::cout << "The Percentage is " << std::fixed << setprecision(1) << shootp << 'n';
这将导致21.9
的输出。仅使用std::setprecision
将导致标准输出的2e+01
。
您需要在之前设置Precision 您执行输出。
cout << "nThe Shooting Percentage is " << setprecision(1) << shootp << endl;
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 如何打印boost多精度128位无符号整数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 为什么输出精度没有正确舍入?
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*