[英]Dynamic memory allocation output
int main () {
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
*pvalue = 29494.99; // Store value at allocated address
cout << "Value of pvalue : " << *pvalue << endl;
delete pvalue; // free up the memory.
return 0;
}
Output:输出:
29495
29495
Why is the output 29495
?为什么输出是
29495
?
When I change the value to 29494.4344
, why is the output 29494.4
?当我将值更改为
29494.4344
,为什么输出是29494.4
?
29494.99
is rounded to 29495.0
for cout <<
purposes because the default number of significant digits with which cout
prints numbers is 6
and zeros at the end are not printed by default.出于
cout <<
目的, 29494.99
被四舍五入为29495.0
,因为cout
打印数字的默认有效位数为6
并且默认情况下不打印末尾的零。
You can use std::setprecision
to change the output precision (requires #include <iomanip>
):您可以使用
std::setprecision
更改输出精度(需要#include <iomanip>
):
cout << setprecision(7) << "Value of pvalue : " << *pvalue << endl;
prints 29494.99
.打印
29494.99
。
29494.4344
is rounded to 29494.4
by default and since the last digit of that is not a zero, it will be printed. 29494.4344
默认四舍五入为29494.4
,因为最后一位不是零,所以会被打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.