Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

convert output from exponentional notation to normal

by Ninke (Novice)
on Mar 19, 2013 at 14:34 UTC ( #1024310=perlquestion: print w/replies, xml ) Need Help??

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


I want my program to produce the division of two variables, this number is always very close to zero:

print $counts / $total ."\n";
The output is written in an exponentional notation:
2.19502183680891e-06 1.46334789120594e-06 8.04841340163266e-06 etc...
could you suggest me the best way to convert them to normal numbers, ex. 0.000002434432? Is there something like "don't use exponential" function that will be valid for all my program?

Thanks in advance.

Replies are listed 'Best First'.
Re: convert output from exponentional notation to normal
by BrowserUk (Pope) on Mar 19, 2013 at 14:41 UTC

    Look up the docs for printf and play with different values for the 20.18 I've used:

    printf "%20.18f\n", 2.19502183680891e-06;; 0.000002195021836809

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: convert output from exponentional notation to normal
by hdb (Monsignor) on Mar 19, 2013 at 14:51 UTC
    If you want to be more flexible, there is the sprintf function which does the same thing as printf but returns a string. Also, you can put the format into a variable for consistency and flexibility across your program.
    $fmt = "%20.18f"; print sprintf( $fmt, 2.19502183680891e-06 )."\n";
    For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken
Re: convert output from exponentional notation to normal
by kennethk (Abbot) on Mar 19, 2013 at 15:58 UTC
    Yes, as both previous posters said, printf/sprintf. The most flexible format choice is %g, which will swap between scientific notation and fixed decimal depending on precision and behaves most like Perl's default. %f means always use fixed decimal and %e means always use scientific.

    In your case, I would probably also round numbers, since I have rarely seen the need for more the 3 significant figures for output outside of scientific computing. So my go to format here would probably "%.3g" for easy digestion, though %.8f is what meets your spec...

    printf "%.8f\n", $counts / $total;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: convert output from exponentional notation to normal
by rjt (Curate) on Mar 19, 2013 at 22:28 UTC

    Bignum will probably do what you want with minimum fuss.

    use 5.012; use bignum; say 1/18690250981623985612839659816239856; __END__ Output: 0.00000000000000000000000000000000005350382940192655191511204088952992 +471243

    You will probably want to read up on bignum for options on how to control accuracy and precision (more info in the related Math::BigInt). These libraries support arbitrary precision, within the limits of available memory, that is.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024310]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2020-10-19 16:27 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (205 votes). Check out past polls.