gpt4 book ai didi

c++ - 如何阻止大 float 以指数形式输出。 (科学计数法?)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:20 24 4
gpt4 key购买 nike

我有一个计算人口增长的程序。它似乎在工作,但在人口超过 100 万之后,它会以十进制数的十次方输出。 (这叫科学记数法吗?指数形式?我忘记了。)

有没有办法把数据输出为一个完整的数字?这是我必须转换它的输出代码。

#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;
}

最佳答案

std::fixed 操纵器,与 std::setprecision 结合使用应该可以帮助你(记得 #include <iomanip> )。

关于c++ - 如何阻止大 float 以指数形式输出。 (科学计数法?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4430976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com