The right way to do this, of course, is in a subroutine, where you pass in your number along with the desired fixed exponent ('-4' in your example). In that case you need to change the '4' to a variable in the $new_exp calculation. You may also want to use sprintf to format the mantissa for your output (maybe you want 500.0 instead of just 500, for example).use strict; use warnings; my $num = 0.05; my $num_string = sprintf("%e", $num); print "$num_string\n"; my($mantissa, $exponent) = split /e/, $num_string; my $new_exp = $exponent + 4; my $new_mantissa = $mantissa * 10**$new_exp; print $new_mantissa, "e-04\n"; __DATA__ 5.000000e-02 500e-04
Updated: You really need
$new_exp = $exponent - $fixed_exponent;
if you want to allow for an arbitrary fixed exponent. My example above applies only to negative values of the fixed exponent.
Never mind. I see now at the end of the OP that ysth supplied a better answer. :-)
In reply to Re: Printing with a Specific Scientific Notation Exponent
by broomduster
in thread Printing with a Specific Scientific Notation Exponent
by behindalens
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |