Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Converting Float to SN notation

by Anonymous Monk
on Mar 24, 2006 at 15:24 UTC ( [id://539047]=perlquestion: print w/replies, xml ) Need Help??

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

Whats the easiest way to convert floats to scientific notation? For example I would like to convert:
3447.0973333 to 3.4470973333e+003 0.5 to 5.0000000000e-001
Thanks!

Replies are listed 'Best First'.
Re: Converting Float to SN notation
by kutsu (Priest) on Mar 24, 2006 at 15:58 UTC

    Check out the docs for sprintf (or perldoc -f sprintf), as a quick example it could be done like: @nums = qw(3447.097333 0.5); print sprintf("%e", $_), "\n" for @nums

      Thanks...that works great...I just had to adjust the precision: print sprintf("%.10e", $_), "\n" for @nums and it works perfectly.

      Thanks again

Re: Converting Float to SN notation
by CountOrlok (Friar) on Mar 24, 2006 at 15:36 UTC
    try sprintf
    -imran

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found