in reply to How to control no. of digits on exponent

The problem is there are 9 characters in the output instead of 8.

No truncation occurs if more characters are produced than requested.

>perl -E"say length sprintf '%5s', $ARGV[0];" abc 5 >perl -E"say length sprintf '%5s', $ARGV[0];" abcdefghi 9

how can I control the number of digits on the exponent so that it prints e-4 or e-04 rather than e-004?

s/e[+-]\K00// # e-4 s/e[+-]\K0// # e-04

Replies are listed 'Best First'.
Re^2: How to control no. of digits on exponent
by mjscott2702 (Pilgrim) on Dec 31, 2010 at 09:51 UTC
    From (s)printf perldoc:

    Note that the number of exponent digits in the scientific notation produced by %e, %E, %g and %G for numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). In other words, 1.23 times ten to the 99th may be either "1.23e99" or "1.23e099".

    So as ikegami points out, you would have to manually strip leading 0's from the exponent.