我有一个计算人口增长的程序。它看起来是有效的,但是当人口超过100万之后,它被输出为一个十进制数的10次方。(这就是所谓的科学符号吗?指数形式?我忘了。)
有没有办法将数据输出为一个完整的数字?下面是输出的代码,我必须对其进行转换。
#include "header.h"
void output (float currentPopulation, float years, float birthRate, float deathRate)
{
cout << "the populaion in " << years << " years will be: " << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl;
} 新代码:
#include "header.h"
void output (float currentPopulation, float years, float birthRate, float deathRate)
{
cout << "the populaion in " << years << " years will be: " << fixed << setprecision(0) << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl;
} 发布于 2010-12-14 00:33:22
与std::setprecision结合使用的std::fixed操纵器应该会对您有所帮助(请记住使用#include <iomanip>)。
发布于 2010-12-14 00:28:50
使用float,你只有24位的精度,大约是16,777,216。如果您要处理的数字大于这个数字并且需要更高的精度,请考虑使用double,不过我认为您仍然需要进行一些格式化才能使其看起来像您想要的那样。在这种情况下,我建议您查看http://www.arachnoid.com/cpptutor/student3.html和http://www.fredosaurus.com/notes-cpp/io/omanipulators.html。
cout << fixed << estimatedPopulation (currentPopulation, years, birthRate, deathRate)发布于 2010-12-14 00:31:58
我建议阅读关于std::scientific的文章:http://msdn.microsoft.com/en-us/library/szb722da(VS.71).aspx
https://stackoverflow.com/questions/4430976
复制相似问题