浮点值未显示准确数字的数字C
floating point values not displaying accurate number of digits c++
为我的$输出提供了正确的数字困难。
每次我插入例如语句中的8时,我都会获得3位数字的数字。10.9虽然我希望它显示$ 10.90
我刚刚添加了SetPrecision,希望它可以解决该问题,无法正常工作
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int Ts; // holds Ts
float Price, Tcost, Total;
cout << "How many shirts you like ?" << endl;
cin >> Ts;
Total = 12 * Ts;
if ( 5 < Ts && Ts < 10)
Tcost = (Total - (.10 * Total));
Price = (Tcost / Ts);
cout << "he cost per shirt is $" << setprecision(4) << Price << " and the total cost is $" << Tcost << setprecision(4) << endl;
return 0;
}
使用std::fixed
的组合(将在setprecision(N)
决定的要打印的点之后设置小数的数量(和std::setprecision(2)
(以便打印两个小数(,并且代码应应现在工作:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int Ts; // holds Ts
float Price, Tcost, Total;
cout << "How many shirts you like ?" << endl;
cin >> Ts;
Total = 12 * Ts;
if ( 5 < Ts && Ts < 10)
Tcost = (Total - (.10 * Total));
Price = (Tcost / Ts); // The indentation is weird here
// but I will leave it as it is
cout << "he cost per shirt is $" << fixed << setprecision(2) << Price << " and the total cost is $" << Tcost << setprecision(2) << endl;
return 0;
}
我从中获得的输出是:
How many shirts you like ?
8
he cost per shirt is $10.80 and the total cost is $86.40
您必须编写std::cout.setf(std::ios::showpoint);
,然后它将起作用。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在 txt 文件中显示前两个数字的程序
- 为什么strlen(s)与s的大小不同,为什么cout-char显示的是字符而不是数字
- 为什么 sf::Text 显示点而不是数字?
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 未来值公式显示疯狂巨大数字的输出
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 无法在C++中显示反转的数字
- 如何执行此程序,将键入的数字显示为数字值的多次
- Cpp代码故障(显示意外数字)
- 如何让我的代码显示文件名不正确或文件中数字的平均值?
- 为什么C++显示错误的数字?
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- C++编译器只显示数字,不显示正确的输出
- 输入的数字与代码显示的数字不同,在结构中使用数组,为什么?
- 如何计算和显示C++中5个数字的平均值?
- 以8位数字形式输入日期并以英文形式显示
- 为什么我的 rand() 数字显示为 0