我声明了一个包含 10 个元素的数组并用 0.0 .. 9.9 初始化它,它的输出是完美的,除了 0.0 已经变成了 0,为什么会这样?
#include <iostream>
using namespace std;
int main(void)
{
int const SIZE = 10;
double number[SIZE] = {0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9};
for(int i(0) ; i < SIZE ; i++)
{
cout << number[i] << endl ;
}
system("PAUSE");
}
谢谢,
输出: 0//它应该是 0.0 而不是 0// 1.1 2.2 . . . 9.9
最佳答案
std::cout
默认情况下将输出最多 6 位数字,在本例中为一(零),因为不需要小数。 std::cout << (double)1.0;
会显示 1
, 例如。您可以使用 std::setprecision
来自 <iomanip>
header 和 std::fixed
保留小数点。
#include <iostream>
#include <iomanip>
int main(void)
{
int const SIZE = 10;
double number[SIZE] = {0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9};
std::cout << std::setprecision(1) << std::fixed;
for(int i(0) ; i < SIZE ; i++)
{
std::cout << number[i] << std::endl ;
}
}
有很多有用的输入/输出操纵器。 Here's a reference to check it out.
关于c++ - 为什么输出显示 0 而不是 0.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053903/