mbhuiyan has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am trying to print the output of a number as follows:

printf("%8.2e\n",0.000123);

This gives me the output in my 32 bit widnows xp as:

1.23e-004

The problem is there are 9 characters in the output instead of 8. So, how can I control the number of digits on the exponent so that it prints e-4 or e-04 rather than e-004?
  • Comment on How to control no. of digits on exponent

Replies are listed 'Best First'.
Re: How to control no. of digits on exponent
by ikegami (Patriarch) on Dec 31, 2010 at 04:31 UTC

    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
      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.

Re: How to control no. of digits on exponent
by Anonymous Monk on Dec 31, 2010 at 04:02 UTC
    You cannot. Observe
    $ perl -le " printf(qq!%.2e\n!,0.000123); " 1.23e-004 $ perl -le " printf(qq!%.1e\n!,0.000123); " 1.2e-004 $ perl -le " printf(qq!%.3e\n!,0.000123); " 1.230e-004
    You will have to use a different approach.
Re: How to control no. of digits on exponent
by Anonymous Monk on Dec 31, 2010 at 03:17 UTC
    Weird.

    I get the same output (1.23e-004) with "perl 5, version 12, subversion 0 (v5.12.0) built for MSWin32-x86-multi-thread" (Strawbery Perl on Vista x64).

    But with "perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int" on the same machine and "perl 5, version 12, subversion 1 (v5.12.1) built for x86_64-linux-thread-multi" in an x64 virtual machine, I get 1.23e-04.

      I suspect that those details of the formatting of a printf string come from the underlying C library's implementation of printf, rather than from perl.

      The printf layout on cygwin matches what I am getting on my linux box, which makes sense, as both use GCC libraries underneath. I would have thought Strawbery Perl would be the same.

      As ikegami said, you could do string processing on the raw output of sprintf, to get the number of digits you want. The problem of course is that the raw number may need all those digits of exponent because it is very large or small.

Re: How to control no. of digits on exponent
by Anonymous Monk on Dec 31, 2010 at 23:47 UTC

    As I understand things, Perl will not guarantee that the output format is what you specify. It will usually fulfill the format, and if it doesn't, it will be close.

    1.23e-004 can easily be edited to fit the requirement, but if you had 1.23e-104, it can't. Editing is clunky, but it works.

    There is a CPAN module which produces FORTRAN formatted strings. It will produce exactly what you want. However, it seems like it has a couple of bugs, which means it won't output everything that a FORTRAN compiled program will handle. Floating point isn't one of the problems from what I remember.