Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scientific Notation Strings to Decimal Scalars

Status
Not open for further replies.

jerehart

Programmer
Jun 10, 1999
61
US
Hey,

I am reading in from a file which has numbers in the format of:

+1.0500000000000000E+004 or
-0.2500000000000000E+003

any idea on a cheap & easy way to change these strings into scalars? Or turn them into the following strings?

10500, -250 ?

SC
 
Here you go...

Code:
use strict;
my $number = '-1.590000E+001';
print "The decimal value of $number is ",
  (sprintf "%.2f", $number),
  ".\n";
Notorious P.I.G.
 
Hey P.I.G.

That is freak'n great! mind if you explain it a little ... ?

SC
 
Sure thing. I'm assuming the part in question is the (sprintf "%.2f", $number) bit, so 'ere goes...

sprintf will format and return a numeric value passed to it, based on the parameters provided between the quotes.

For your example, I passed the value $number to sprintf and 'asked' it to return the number in decimal format (the f 'switch') with 2 numbers following the decimal point (.2).

There are many parameters which you can use, and far more detailed and complex ways to use sprintf, so for a REAL explanation and a ton of good examples, check out this page:
[hth] Notorious P.I.G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top