Print float or double in non scientific notaion

2019-07-29 03:58发布

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.

2条回答
狗以群分
2楼-- · 2019-07-29 04:06

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

查看更多
唯我独甜
3楼-- · 2019-07-29 04:30

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.

查看更多
登录 后发表回答