Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Printing with a Specific Scientific Notation Exponent

by broomduster (Priest)
on Sep 18, 2008 at 18:47 UTC ( [id://712363]=note: print w/replies, xml ) Need Help??


in reply to Printing with a Specific Scientific Notation Exponent

I think you need to roll your own for this. The following will get you started:
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
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).

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://712363]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found