Display a number decimal format instead as an exponential in cout

自闭症网瘾萝莉.ら 提交于 2021-02-05 11:56:21

问题


I calculated a total of floats and I got a number like 509990e-405. I'm assuming this is the short version; how can I cout this as a full number?

   cout << NASATotal << endl;

is what I have now.


回答1:


You can force the output to be not in scientific notation, and to have the sufficient precision to show your small number.

#include <iomanip>

// ...

long double d = 509990e-405L;
std::cout << std::fixed << std::setprecision(410) << d << std::endl;

Output:

0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050999000000

If you really want this is another question.




回答2:


You can write your own BigNumber class that stores the results as strings. You would have to implement all of your numeric operations and I'm guessing performance will be an issue. But it can be done, no problem -- assuming that is what you want.



来源:https://stackoverflow.com/questions/5998367/display-a-number-decimal-format-instead-as-an-exponential-in-cout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!