Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

simple!!

by vnpandey (Scribe)
on Aug 18, 2000 at 12:47 UTC ( [id://28447]=perlquestion: print w/replies, xml ) Need Help??

vnpandey has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks..I want to write some number contained inside a scalar in exponential form for ex if the no is 122.34 I want it as 1.2234e(+2) when I ask it to print this scalar .. Please help...Thanks-VNPandey

Replies are listed 'Best First'.
RE: simple!!
by t0mas (Priest) on Aug 18, 2000 at 13:34 UTC
    How about:

    printf("%e",122.34);

    or if you want it exactly the way you said:
    $_=sprintf("%#1.4e",122.34); s/e([+-])(.*)/"e($1".int($2).")"/e; print;


    /brother t0mas
      Just to take it farther... to undo this, you could:

      s/([\.\d]+)e\(?([-+]?\d+)\)?/$1*10**$2/egi;

      I would rather have the notation given as 1.2234e+2, simply because Perl sees that as a number. By that I mean if you:

      my $foo = 1.2234e(+2);

      Perl will complain. If you do the following, it won't:

      my $foo = 1.2234e+2;

      The above RE should handle both cases. This is fun though..

      $_=sprintf("%#1.4e",122.34); print; s/([\.\d]+)e\(?([-+]?\d+)\)?/$1*10**$2/egi; print $/; print;

      Cheers,
      KM

        A very nice undo regexp there...

        I agree with you KM that 1.2234e+2 is a better format for keeping and operations, but for printing (as I believe vnpandey wanted) I rather use the format that pays my bills :)


        /brother t0mas
Re: simple!!
by davorg (Chancellor) on Aug 18, 2000 at 12:50 UTC

    You can do this kind of conversion (and a whole bunch more) using the Number::Format module from CPAN.

    Update

    Don't know why everyone's been voting this up. It originally said Date::Format which is clearly nonsense :( Thanks to merlyn for pointing it out.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>

Log In?
Username:
Password:

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

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

    No recent polls found