Welcome to WebmasterWorld Guest from 126.96.36.199
$i = $_;
$o = eval($i);
printf ("the answer is %.3g", $o);
# $o = output answer $i = input
if you type 10000/3 you get "the answer is 3.33e+003"
what can i do so that there are no unecessary 0s in the exponent?
[edited by: phranque at 10:17 pm (utc) on July 22, 2008]
[edit reason] disabled smileys ;) [/edit]
The correct answer, I believe, is no.
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".
(The format arguments for printf are explained in sprintf.)
and would that mean that the formatting printed locally in the command line window might be different from the formatting printed by a server hosting a web page?
After all, PERL stands for: Practical Extraction (and) Reporting Language
$answer = sprintf("%.3g", $o);
$answer =~ s/(.*)(\+[^0]*)(0+)([^0]*)/$1$2$4/;
print "the answer is $answer";
>the answer is 3.33e+3
Directly from sprintf/printf is one thing, treating it as a string is another case altogether. :-)