Strange - need a little help

O

Oli Scheit

Hi, all!

I have the following problem:
on one linux box the line
$foo = 10 / 3;
brings $foo to 3.333333e+something
and a
print sprintf '%.2f', $foo;
fails due to the e+something.

On my private linux box,
the line
$foo = 10 / 3;
brings $foo to something like 3.3333333

Probably different versions of Perl.

Is there any way to convert the x.xxxxe+y
to a formatted float?

regards,
Oli
 
G

Gunnar Hjalmarsson

Oli said:
I have the following problem:
on one linux box the line
$foo = 10 / 3;
brings $foo to 3.333333e+something
and a
print sprintf '%.2f', $foo;
fails due to the e+something.

On my private linux box,
the line
$foo = 10 / 3;
brings $foo to something like 3.3333333

Probably different versions of Perl.

Is there any way to convert the x.xxxxe+y
to a formatted float?

On my (Windows) box this works fine:

$foo = sprintf '%e', 10 / 3;
printf '%.2f', $foo;

If you don't need $foo to be very exact in the first place, you can
avoid the scientific notation by doing:

$foo = sprintf '%f', 10 / 3;
 
A

Anno Siegel

Oli Scheit said:
Hi, all!

I have the following problem:
on one linux box the line

What version of linux. What version of Perl?
$foo = 10 / 3;
brings $foo to 3.333333e+something
^^^^^^^^^^

What is "something"? Please give us complete information. Even if you
think a detail is irrelevant -- since you obviously don't understand
the problem, it may not be. In this case it would be interesting if
the exponent is the correct one, or if it is even numeric.
and a
print sprintf '%.2f', $foo;
fails due to the e+something.

If "something" is a (nonnegative integer) number it won't fail. What
is "something"?
On my private linux box,

What version? What Perl version?
the line
$foo = 10 / 3;
brings $foo to something like 3.3333333

Probably different versions of Perl.

Probably, eh? No one but you can find out.
Is there any way to convert the x.xxxxe+y
to a formatted float?

Yes, sprintf. If the exponent is legal.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top