converting exponential format number to decimal format number

Discussion in 'Perl Misc' started by Fei Liu, Dec 14, 2006.

  1. Fei Liu

    Fei Liu Guest

    Hi group, is there a quick way to convert an exponential format number
    to decimal format number. For example,

    13.534e+10 = 1353400000

    I can come up a perl function but it's not perly. Can I get some help
    please? Thanks,
    Fei Liu, Dec 14, 2006
    #1
    1. Advertising

  2. Fei Liu

    John Bokma Guest

    "Fei Liu" <> wrote:

    > 13.534e+10


    perl -e "print 13.534e+10"
    135340000000

    perl -e "my $var = 13.534e+10; print length $var"
    12

    So at least here (WinXP+ActiveState) Perl does this internally.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Dec 14, 2006
    #2
    1. Advertising

  3. Fei Liu

    Guest

    Fei Liu wrote:
    > Hi group, is there a quick way to convert an exponential format number
    > to decimal format number. For example,
    >
    > 13.534e+10 = 1353400000


    How about this:

    print 13.534e+10;

    #prints 135340000000

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Dec 14, 2006
    #3
  4. Fei Liu

    Fei Liu Guest

    John Bokma wrote:
    > "Fei Liu" <> wrote:
    >
    > > 13.534e+10

    >
    > perl -e "print 13.534e+10"
    > 135340000000
    >
    > perl -e "my $var = 13.534e+10; print length $var"
    > 12
    >
    > So at least here (WinXP+ActiveState) Perl does this internally.
    >


    Thanks for your input, but try 13.534e+26, you will find perl prints
    13.534e+26. It's part of the code
    where it reads this number from a file and the output needs to be
    converted to decimal format for another application (say myapp) to use.
    Unfortunately, myapp only understands decimal format number.
    Fei Liu, Dec 14, 2006
    #4
  5. Fei Liu

    J. Gleixner Guest

    Fei Liu wrote:
    > John Bokma wrote:
    >> "Fei Liu" <> wrote:
    >>
    >>> 13.534e+10

    >> perl -e "print 13.534e+10"
    >> 135340000000
    >>
    >> perl -e "my $var = 13.534e+10; print length $var"
    >> 12
    >>
    >> So at least here (WinXP+ActiveState) Perl does this internally.
    >>

    >
    > Thanks for your input, but try 13.534e+26, you will find perl prints
    > 13.534e+26.


    I find it prints 1.3534e+27

    Look at: perldoc bigint
    J. Gleixner, Dec 14, 2006
    #5
  6. Fei Liu

    Guest

    Fei Liu wrote:
    > Thanks for your input, but try 13.534e+26, you will find perl prints
    > 13.534e+26. It's part of the code


    use Math::BigInt;
    my $int = Math::BigInt->new('13.534e+26');
    print $int->as_int();

    #prints 1353400000000000000000000000


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Dec 14, 2006
    #6
  7. Fei Liu

    Guest

    wrote:
    > Fei Liu wrote:
    > > Thanks for your input, but try 13.534e+26, you will find perl prints
    > > 13.534e+26. It's part of the code

    >
    > use Math::BigInt;
    > my $int = Math::BigInt->new('13.534e+26');
    > print $int->as_int();


    Or, if you don't mind there being some 9's way out at the end,

    printf "%f", 13.534e26

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Dec 14, 2006
    #7
  8. Fei Liu

    Guest

    wrote:
    > my $int = Math::BigInt->new('13.534e+26');
    > print $int->as_int();


    Or, if you are just doing the one conversion and don't need to retain
    the constructor:

    print Math::BigInt->new('13.534e+26')->as_int();

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Dec 14, 2006
    #8
  9. Fei Liu

    John Bokma Guest

    "Fei Liu" <> wrote:

    > John Bokma wrote:
    >> "Fei Liu" <> wrote:
    >>
    >> > 13.534e+10

    >>
    >> perl -e "print 13.534e+10"
    >> 135340000000
    >>
    >> perl -e "my $var = 13.534e+10; print length $var"
    >> 12
    >>
    >> So at least here (WinXP+ActiveState) Perl does this internally.
    >>

    >
    > Thanks for your input, but try 13.534e+26


    So, you gave a bad example. Always make your problem description as
    complete as possible and provide examples that show your specific problem.
    This way people can help you better, and you don't waste a lot of time of
    other people.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Dec 14, 2006
    #9
  10. Fei Liu

    Dr.Ruud Guest

    Fei Liu schreef:

    > Hi group, is there a quick way to convert an exponential format
    > number to decimal format number. For example,
    >
    > 13.534e+10 = 1353400000
    >
    > I can come up a perl function but it's not perly.


    This works in a limited way:

    perl -we 'printf "%.0f\n", q/9.64e+21/'
    9640000000000000000000

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 14, 2006
    #10
  11. Fei Liu

    Fei Liu Guest

    On Dec 14, 6:07 pm, John Bokma <> wrote:
    > "Fei Liu" <> wrote:
    > > John Bokma wrote:
    > >> "Fei Liu" <> wrote:

    >
    > >> > 13.534e+10

    >
    > >> perl -e "print 13.534e+10"
    > >> 135340000000

    >
    > >> perl -e "my $var = 13.534e+10; print length $var"
    > >> 12

    >
    > >> So at least here (WinXP+ActiveState) Perl does this internally.

    >
    > > Thanks for your input, but try 13.534e+26So, you gave a bad example. Always make your problem description as

    > complete as possible and provide examples that show your specific problem.
    > This way people can help you better, and you don't waste a lot of time of
    > other people.
    >

    Your time is appreciated.
    Fei Liu, Dec 14, 2006
    #11
  12. Fei Liu

    Ch Lamprecht Guest

    wrote:
    > wrote:
    >
    >> my $int = Math::BigInt->new('13.534e+26');
    >> print $int->as_int();

    >
    >
    > Or, if you are just doing the one conversion and don't need to retain
    > the constructor:
    >
    > print Math::BigInt->new('13.534e+26')->as_int();


    as_int returns a Math::BigInt object. We have one already.

    print Math::BigInt->new('13.534e+26')



    Christoph

    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Dec 14, 2006
    #12
  13. Fei Liu <> wrote:
    >
    > John Bokma wrote:
    >> "Fei Liu" <> wrote:
    >>
    >> > 13.534e+10

    >>
    >> perl -e "print 13.534e+10"
    >> 135340000000
    >>
    >> perl -e "my $var = 13.534e+10; print length $var"
    >> 12
    >>
    >> So at least here (WinXP+ActiveState) Perl does this internally.
    >>

    >
    > Thanks for your input, but try 13.534e+26, you will find perl prints
    > 13.534e+26. It's part of the code
    > where it reads this number from a file and the output needs to be

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    Then it is not a number!

    It is a string.


    > converted to decimal format for another application (say myapp) to use.



    Perl's regular expressions are handy for working on string data:


    $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 15, 2006
    #13
  14. Fei Liu

    John Bokma Guest

    Tad McClellan <> wrote:

    >>> "Fei Liu" <> wrote:
    >>>
    >>> > 13.534e+10


    [..]

    > $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;


    nify :-D

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Dec 15, 2006
    #14
  15. Fei Liu

    Fei Liu Guest

    On Dec 14, 8:26 pm, Tad McClellan <> wrote:
    > Fei Liu <> wrote:
    >
    > > John Bokma wrote:
    > >> "Fei Liu" <> wrote:

    >
    > >> > 13.534e+10

    >
    > >> perl -e "print 13.534e+10"
    > >> 135340000000

    >
    > >> perl -e "my $var = 13.534e+10; print length $var"
    > >> 12

    >
    > >> So at least here (WinXP+ActiveState) Perl does this internally.

    >
    > > Thanks for your input, but try 13.534e+26, you will find perl prints
    > > 13.534e+26. It's part of the code
    > > where it reads this number from a file and the output needs to be ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > Then it is not a number!
    >
    > It is a string.
    >
    > > converted to decimal format for another application (say myapp) to use.Perl's regular expressions are handy for working on string data:

    >
    > $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;
    >


    This one is neat, I'll use it. Thanks!
    Fei Liu, Dec 15, 2006
    #15
  16. Fei Liu

    Fei Liu Guest

    On Dec 14, 8:26 pm, Tad McClellan <> wrote:
    > Fei Liu <> wrote:
    >
    > > John Bokma wrote:
    > >> "Fei Liu" <> wrote:

    >
    > >> > 13.534e+10

    >
    > >> perl -e "print 13.534e+10"
    > >> 135340000000

    >
    > >> perl -e "my $var = 13.534e+10; print length $var"
    > >> 12

    >
    > >> So at least here (WinXP+ActiveState) Perl does this internally.

    >
    > > Thanks for your input, but try 13.534e+26, you will find perl prints
    > > 13.534e+26. It's part of the code
    > > where it reads this number from a file and the output needs to be ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > Then it is not a number!
    >
    > It is a string.
    >
    > > converted to decimal format for another application (say myapp) to use.Perl's regular expressions are handy for working on string data:

    >
    > $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;
    >


    Hmm, there appears to be a bug in this expression, it's assuming the
    number string starts with \., for example
    perl -e '$str = '1.2345e+10'; $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x
    ($2 - length $1) /ie; print $str;'
    12345000000

    which is wrong.
    Fei Liu, Dec 15, 2006
    #16
  17. Fei Liu

    Ben Morrow Guest

    Quoth Tad McClellan <>:
    <snip>
    > >> "Fei Liu" <> wrote:
    > >>
    > >> > 13.534e+10

    >
    > Perl's regular expressions are handy for working on string data:
    >
    >
    > $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;


    Fails for '1.123456e+5'. :)

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." [C.S.Lewis]
    Ben Morrow, Dec 15, 2006
    #17
  18. Ben Morrow <> wrote:
    > Quoth Tad McClellan <>:
    ><snip>
    >> >> "Fei Liu" <> wrote:
    >> >>
    >> >> > 13.534e+10

    >>
    >> Perl's regular expressions are handy for working on string data:
    >>
    >>
    >> $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;

    >
    > Fails for '1.123456e+5'. :)



    Heck, it fails for simple ol' 1.1e-2, even without searching
    through the edges of silliness like you did. :)

    If I did _all_ of his programming for him, I'd have to get his paycheck...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 16, 2006
    #18
  19. Jim Gibson <> wrote:
    > In article <>, Ben Morrow
    ><> wrote:
    >
    >> Quoth Tad McClellan <>:
    >> <snip>
    >> > >> "Fei Liu" <> wrote:
    >> > >>
    >> > >> > 13.534e+10
    >> >
    >> > Perl's regular expressions are handy for working on string data:
    >> >
    >> >
    >> > $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;

    >>
    >> Fails for '1.123456e+5'. :)

    >
    > Fixed (for that case, anyway) with:
    >
    > $str =~ s{\A 0* (\d*) \. (\d+) e [+]? (\d+) }
    > {$1 . (($3 > length $2 ) ?
    > ($2 . '0' x ($3 - length $2)) :
    > (substr($2,0,$3) . '.' . substr($2,$3)))}xie;
    >
    >
    > which produces '112345.6', but is getting a bit cumbersome.

    ^^^^^

    bits must be pretty big where you come from. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 16, 2006
    #19
  20. Fei Liu <> wrote:
    > On Dec 14, 8:26 pm, Tad McClellan <> wrote:



    >> $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x ($2 - length $1) /ie;
    >>

    >
    > Hmm, there appears to be a bug in this expression,



    It is not "apparent" to me...


    > it's assuming the
    > number string starts with \.,



    No it isn't.

    It is assuming that the *part that needs to be changed* starts with \.

    The part before the dot is left as it is.


    > for example
    > perl -e '$str = '1.2345e+10'; $str =~ s/\.(\d+)e[+](\d+)/ $1 . '0' x
    > ($2 - length $1) /ie; print $str;'
    > 12345000000
    >
    > which is wrong.



    Errr, what would you have the right answer be then?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 16, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Lawrence [MSFT]

    Re: See data in exponential format

    Eric Lawrence [MSFT], Mar 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    567
    =?Utf-8?B?Sm9yZ2UgTWFnYW50bw==?=
    Mar 2, 2004
  2. ruds
    Replies:
    3
    Views:
    5,567
    Daniel Pitts
    Jan 10, 2007
  3. sweetone
    Replies:
    1
    Views:
    407
    Andrew Thompson
    Jan 20, 2007
  4. Michael McGarry
    Replies:
    4
    Views:
    871
    Michael McGarry
    Nov 2, 2005
  5. Oscar Lok
    Replies:
    1
    Views:
    163
    David Kastrup
    Nov 19, 2006
Loading...

Share This Page