# converting exponential format number to decimal format number

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

1. ### Fei LiuGuest

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

2. ### John BokmaGuest

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

3. ### usenetGuest

How about this:

print 13.534e+10;

#prints 135340000000

usenet, Dec 14, 2006
4. ### Fei LiuGuest

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
5. ### J. GleixnerGuest

I find it prints 1.3534e+27

Look at: perldoc bigint

J. Gleixner, Dec 14, 2006
6. ### usenetGuest

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

#prints 1353400000000000000000000000

usenet, Dec 14, 2006
7. ### xhosterGuest

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
8. ### usenetGuest

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
9. ### John BokmaGuest

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
10. ### Dr.RuudGuest

Fei Liu schreef:
This works in a limited way:

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

Dr.Ruud, Dec 14, 2006
11. ### Fei LiuGuest

Your time is appreciated.

Fei Liu, Dec 14, 2006
12. ### Ch LamprechtGuest

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

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

Christoph

Ch Lamprecht, Dec 14, 2006
13. ### Tad McClellanGuest

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

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
14. ### John BokmaGuest

nify :-D

John Bokma, Dec 15, 2006
15. ### Fei LiuGuest

This one is neat, I'll use it. Thanks!

Fei Liu, Dec 15, 2006
16. ### Fei LiuGuest

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
17. ### Ben MorrowGuest

Fails for '1.123456e+5'.

Ben

Ben Morrow, Dec 15, 2006
18. ### Tad McClellanGuest

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
19. ### Tad McClellanGuest

^^^^^

bits must be pretty big where you come from.

Tad McClellan, Dec 16, 2006
20. ### Tad McClellanGuest

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

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.