在代码块中使用双精度给了我 int 输出
Use of double in codeblocks gives me int output
#include <iostream> using namespace std;
int main()
{
double x=5.0,y=4.0,z;
z=x+y;
cout<<x<<endl<<y<<endl<<z;
return 0;
}
上面的程序给了我以下输出:549当我将变量声明为双精度,甚至 z 为双精度时,为什么我会将输出为整数值 (9)?
cout
在这里很有帮助:如果double
值是整数,则默认情况下,它不显示小数分隔符,后跟任意数量的零。
如果您想显示与平台上特定double
具有的精度一样多的数字,请在以下行中使用一些内容
cout.precision(std::numeric_limits<double>::max_digits10);
cout << fixed << x << endl;
浮
点后没有数字的浮点数默认打印为整数。
要始终显示浮点数,请使用 setiosflags(ios::showpoint)
。
您可以将其与 fixed
和 setprecision(n)
I/O 标志结合使用,以限制浮点后要打印的位数。例如:
double d = 5.0;
cout << setiosflags(ios::showpoint) << d << endl; // prints 5.00000
cout << setiosflags(ios::showpoint) << fixed << setprecision(1)
<< d << endl; // prints 5.0
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- std::cout输出int时出现编译错误
- C++ 中文件输入输出中的长长 v/s int
- 我从int x[3]得到的一个非常奇怪的输出;
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 在MSDOS编译器上,用大于最大值的值初始化int时输出混乱
- 有没有一种简单的方法可以将矢量 int 集转换为字符串输出?
- 基本 int 数组提供意外输出
- 为什么printf和cout为此无符号int提供了不同的输出
- Int 输出与给定值不对应
- 为什么没有"int"类型声明时输出不同?
- 当用户输入int变量的字符串时,输出
- 如何使用字符获取多个输入,使用 int 获取多个输出?C++
- unsigned int/signed int/long-long:无法解释的输出
- 矢量<int>的赋值和输出向量
- 程序的输出,在 C++ 中,&int 到 char* 转换
- 将 64 位 int 作为输出传递到 32 位内联 asm
- 在代码块中使用双精度给了我 int 输出
- Void与Int输出参数签名
- C++如何将int输出为32位二进制