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. Advertisements

  2. Fei Liu

    John Bokma Guest

    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 Bokma, Dec 14, 2006
    #2
    1. Advertisements

  3. Fei Liu

    usenet Guest

    How about this:

    print 13.534e+10;

    #prints 135340000000
     
    usenet, Dec 14, 2006
    #3
  4. Fei Liu

    Fei Liu Guest

    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

    I find it prints 1.3534e+27

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

    usenet Guest

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

    #prints 1353400000000000000000000000
     
    usenet, Dec 14, 2006
    #6
  7. Fei Liu

    xhoster Guest

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

    printf "%f", 13.534e26

    Xho
     
    xhoster, Dec 14, 2006
    #7
  8. Fei Liu

    usenet Guest

    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();
     
    usenet, Dec 14, 2006
    #8
  9. Fei Liu

    John Bokma Guest

    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 Bokma, Dec 14, 2006
    #9
  10. Fei Liu

    Dr.Ruud Guest

    Fei Liu schreef:
    This works in a limited way:

    perl -we 'printf "%.0f\n", q/9.64e+21/'
    9640000000000000000000
     
    Dr.Ruud, Dec 14, 2006
    #10
  11. Fei Liu

    Fei Liu Guest

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

    Ch Lamprecht Guest

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

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



    Christoph
     
    Ch Lamprecht, Dec 14, 2006
    #12
  13. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    Then it is not a number!

    It is a string.


    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, Dec 15, 2006
    #13
  14. Fei Liu

    John Bokma Guest

    nify :-D
     
    John Bokma, Dec 15, 2006
    #14
  15. Fei Liu

    Fei Liu Guest

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

    Fei Liu Guest

    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

    Fails for '1.123456e+5'. :)

    Ben
     
    Ben Morrow, Dec 15, 2006
    #17

  18. 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, Dec 16, 2006
    #18
  19. ^^^^^

    bits must be pretty big where you come from. :)
     
    Tad McClellan, Dec 16, 2006
    #19

  20. It is not "apparent" to me...


    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.


    Errr, what would you have the right answer be then?
     
    Tad McClellan, Dec 16, 2006
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.