On Mon, Dec 22, 2003 at 11:17:03AM -0800, Perl wrote:
> i know what " %9.1f" would have done in this case.
> but how does " %9.2g" round off ?
The *rounding* works like "%f", but there are some other
differences.
a) the precision (".2") applies to significant digits, not
digits after the decimal point
% perl -e 'printf "%.2g\n", 2.25'
2.2
b) trailing zeros will be suppressed
% perl -e 'printf "%.2g\n", 2.05'
2
c) the output will be given in scientific notation if there
are more digits to the left of the decimal point than the
specified number of significant digits
% perl -e 'printf "%.2g\n", 225'
2.2e+02
Perl doesn't actually implement %g itself, so check your
system's sprintf(3) or gconvert/gcvt manpages for a more
precise description.
And incidentally, when you do:
% perl -le 'print 100/3'
33.3333333333333
Perl uses "%g" (or gcvt()) to format the floating point value:
% perl -le 'print sprintf "%.15g", 100/3'
33.3333333333333
--
Steve
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>