# 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

Fei Liu, Dec 14, 2006

2. ### John BokmaGuest

"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

3. ### 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

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
4. ### Fei LiuGuest

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

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
6. ### 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
7. ### 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

--
Usenet Newsgroup Service \$9.95/Month 30GB

, Dec 14, 2006
8. ### 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
9. ### John BokmaGuest

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

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
11. ### Fei LiuGuest

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

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

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

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;

--
Perl programming
Fort Worth, Texas

14. ### John BokmaGuest

>>> "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
15. ### Fei LiuGuest

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
16. ### Fei LiuGuest

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

<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

Ben Morrow <> wrote:
><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...

--
Perl programming
Fort Worth, Texas

Jim Gibson <> wrote:
> In article <>, Ben Morrow
><> wrote:
>
>> <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.

--
Perl programming
Fort Worth, Texas

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?

--