How does% g round floating point numbers?

While I use %gthe format c as a specifier printf(), sometimes it is rounded to 2 places after the decimal point, sometimes to 3 places, sometimes to 4 places ... how does this happen?

Actually, where should we use %ginstead of %for %efor floating point numbers?

+3
source share
4 answers

The format specifier %gdoes its rounding exactly as it does, %fbut if it %fresults in 4.234000, then it %gwill omit trailing zeros and print 4.234.

%g , , 12345.6, 1.235e04.

+5

%g "" %e %f , , , . , %g .

+5

%f "" , %g .
6 .

+4

printf:

" f e ( F E             G). . , 6 ;              , 1. e              -4              .              ;              .

"RTFM", , , , .

+3

Source: https://habr.com/ru/post/1767529/


All Articles