newbie question : about the perl sprintf

2009-10-21 Thread Majian
Hi, all ;

   I want to print  this sentence " The number in scientific
notation is 1.255000e+02".

 So   I write a perl script like this :
   #!/usr/bin/perl
sprintf "The number in scientific notation is %e", 1.255;
   But  the screen output is "The number in scientific notation is
1.255000e+00"

And  I modify it like this "sprintf "The number in scientific
notation is %e", 01.255;"
The screen now output is " The number in scientific notation
is 1.255000e+03"


   I don't know how I can display the correct answer .

  Please forgive me if this is a very simple question on Perl . I am a
Perl newbie ~~~


  Thanks ~~~


Re: newbie question : about the perl sprintf

2009-10-21 Thread Jim Gibson

At 8:52 PM +0800 10/21/09, Majian wrote:

Hi, all ;

   I want to print  this sentence " The number in scientific
notation is 1.255000e+02".

 So   I write a perl script like this :
   #!/usr/bin/perl
sprintf "The number in scientific notation is %e", 1.255;
   But  the screen output is "The number in scientific notation is
1.255000e+00"


I don't get anything when I execute that line. You must have used the 
printf function in your actual code.


1.255000e+00 is the correct scientific representation of the number 
1.255. If you want to print 1.255000e+02, then you should print the 
number 125.5.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question : about the perl sprintf

2009-10-21 Thread Christian Bernini
>
> sprintf "The number in scientific notation is %e", 1.255;
>

Sprintf doesn't print actually, it just returns a string based on the
formats provided in the list you have.

To actually print something you should use printf.

printf "The number in scientific notation is %e",1255;


And the notation 1.255000e+03 is right as the output since it means 1.255000
* (10^3), so

1.255 * 1000 = 1255.

(as Jim already said)

Att.
Christian Bernini


Re: newbie question : about the perl sprintf

2009-10-22 Thread uap12
On 21 Okt, 14:52, jian...@gmail.com (Majian) wrote:
> Hi, all ;
>
>            I want to print  this sentence " The number in scientific
> notation is 1.255000e+02".
>
>          So   I write a perl script like this :
>                #!/usr/bin/perl
>                 sprintf "The number in scientific notation is %e", 1.255;
>            But  the screen output is "The number in scientific notation is
> 1.255000e+00"
>
>             And  I modify it like this "sprintf "The number in scientific
> notation is %e", 01.255;"
>                 The screen now output is " The number in scientific notation
> is 1.255000e+03"
>
>            I don't know how I can display the correct answer .
>
>       Please forgive me if this is a very simple question on Perl . I am a
> Perl newbie ~~~
>
>           Thanks ~~~

#!/usr/bin/perl
printf("The number in scientific notation is %e\n", 125.55);
Gives The number in scientific notation is 1.255500e+002

I test to enter the result you are looking for as

perl -42 de  now ju can test single line of Perl kode

heter i enter print 1.255500e+02

( CTRL-Z) takes you out, (att least in WIndows version of PERL)

You can read more about formating data in this link
http://perldoc.perl.org/functions/sprintf.html

Best regards
Anders


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question : about the perl sprintf

2009-10-24 Thread Peter Scott
On Wed, 21 Oct 2009 20:52:05 +0800, Majian wrote:
> And  I modify it like this "sprintf "The number in
> scientific
> notation is %e", 01.255;"
> The screen now output is " The number in scientific
> notation
> is 1.255000e+03"

Ha, this is an interesting case.  By putting the zero before the 1, it 
turns it into an octal number and now the period becomes the concatenation 
operator instead of a decimal point, yielding a term of 1255.

Try printf "The number in scientific notation is %e", 037.255"; and see 
what happens.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question : about the perl sprintf

2009-10-25 Thread Majian
I found these :
perl -e'print 01.234 + 01.234', "\n"'
perl -e'print 01.234 + 011.234' "\n"'
perl -e'print 01.234.12 + 01.234', "\n"'

And the results were :

1235234
1235234
1235.12234



Can someone explain it ?

Thanks~~


On Sat, Oct 24, 2009 at 7:28 PM, Peter Scott  wrote:

> On Wed, 21 Oct 2009 20:52:05 +0800, Majian wrote:
> > And  I modify it like this "sprintf "The number in
> > scientific
> > notation is %e", 01.255;"
> > The screen now output is " The number in scientific
> > notation
> > is 1.255000e+03"
>
> Ha, this is an interesting case.  By putting the zero before the 1, it
> turns it into an octal number and now the period becomes the concatenation
> operator instead of a decimal point, yielding a term of 1255.
>
> Try printf "The number in scientific notation is %e", 037.255"; and see
> what happens.
>
> --
> Peter Scott
> http://www.perlmedic.com/
> http://www.perldebugged.com/
> http://www.informit.com/store/product.aspx?isbn=0137001274
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: newbie question : about the perl sprintf

2009-10-25 Thread Philip Potter
2009/10/25 Majian :
> I found these :
> perl -e'print 01.234 + 01.234', "\n"'

print (01).(234+01).234, "\n";

this evaluates to '1'.'235'.'234'

> perl -e'print 01.234 + 011.234' "\n"'

I didn't get 1235234, I got 1243234.
print (01).(234+011).(234),"\n"
evaluates to
print '1'.(234+9).'234',"\n";
evaluates to
print '1'.'243'.'234',"\n";

> perl -e'print 01.234.12 + 01.234', "\n"'

I'll let you work this one out as an exercise. The key point is that
you can't have decimal octal numbers, so the . operator is interpreted
as the string concatenation operator.

Phil

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question : about the perl sprintf

2009-10-25 Thread Dr.Ruud

Majian wrote:

I found these :
perl -e'print 01.234 + 01.234', "\n"'
perl -e'print 01.234 + 011.234' "\n"'
perl -e'print 01.234.12 + 01.234', "\n"'

And the results were :

1235234
1235234
1235.12234


For other surprises, try also:

perl -wle 'print length(01.234.12)'
perl -wle 'print 01.234.12'

perl -wle 'print length(1.234.12)'
perl -wle 'print length(0+1.234.12)'

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/