Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Decimal to Scientific notation

by Not_a_Number (Prior)
on Mar 24, 2015 at 19:08 UTC ( [id://1121200]=note: print w/replies, xml ) Need Help??


in reply to Decimal to Scientific notation

All the answers given so far are fine if there are 11* digits after the decimal point. But what if the mantissa is longer than 11 digits?:

printf "%.11e\n", 0.99982928833123; # 9.99829288331e-001

Still truncated...

And what if there are considerably fewer digits:

printf "%.11e\n", 0.999; # 9.9980000000e-001

Would the OP want all those extra zeroes?

One solution (admittedly not very elegant):

my $dec = 0.99982928833123; my $len = length( ( split /\./, $dec )[1] ); printf "%.${len}e\n", $dec; # 9.99829288331230e-001

* Well, or 12 in this case.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found