Print float or double in non scientific notaion

走远了吗. 提交于 2019-12-11 08:28:42

问题


Float or double always gives answer in scientific notation if number of digits are 7 or more. Like an decimal number 10000000.5 it is giving 1e-08 something. I am wondering if we can print 10000000.5 without adding any new header file.


回答1:


If you are printing to cout, use

std::cout.setf( std::ios::fixed, std::ios::floatfield );

See it work.

You might also want std::cout.precision(1) to set the number of digits after the decimal point.




回答2:


printf("%.1f", someFloat) should do that for you, if you want one decimal digit. If you want n decimal digits, then use %.nf



来源:https://stackoverflow.com/questions/16370596/print-float-or-double-in-non-scientific-notaion

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