本文介绍了怎样%G向上舍浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用%G 的格式说明在的printf(),有时四舍五入高达2小数点后的地方,有时高达3个地方,someimes高达4个地方......怎么这样做?

While I'm using %g as format specifier in printf(), sometimes it rounds up upto 2 places after decimal point, sometimes upto 3 places , someimes upto 4 places...how it does so?

其实,我们应该用%G 的而不是%F %E 浮点数?

Actually where we should use %g instead of %f or %e for floating point numbers?

推荐答案

%G 格式说明做它的舍入就像%F 会做,但如果%F 将导致 4.234000 ,那么 %G 将省略尾随零和打印 4.234

The %g format specifier does its rounding just like %f would do, but if %f would result in 4.234000, then %g will omit the trailing zeros and print 4.234.

%G 时,最有意义的是一些数字印刷作为 12345.6 ,而一个稍微大一些会>打印为 1.235e04

%g should be used when it makes the most sense in your output format that some numbers are printed as 12345.6, while a slightly bigger number would be printed as 1.235e04.

这篇关于怎样%G向上舍浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 18:07