### convert output from exponentional notation to normal

by Ninke (Novice)
 on Mar 19, 2013 at 14:34 UTC Need Help??
Ninke has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

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?

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 (Prior) 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 (Deacon) 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.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024310]
Front-paged by Corion
help
Chatterbox?
 [marto]: Discipulus Let's discuss Podmaster's Signature and the response from tye [Discipulus]: thanks marto i'll read it after an urgent phonecall

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-06-23 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (539 votes). Check out past polls.