converting exponential format number to decimal format number

F

Fei Liu

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,
 
J

John Bokma

Fei Liu said:
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.
 
U

usenet

Fei said:
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
 
F

Fei Liu

John said:
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.
 
U

usenet

Fei said:
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
 
X

xhoster

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
 
J

John Bokma

Fei Liu said:
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.
 
D

Dr.Ruud

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
 
F

Fei Liu

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

Ch Lamprecht

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
 
T

Tad McClellan

Fei Liu said:
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;
 
F

Fei Liu

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!
 
F

Fei Liu

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

Ben Morrow

Quoth Tad McClellan said:
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
 
T

Tad McClellan

Ben Morrow said:
Quoth Tad McClellan said:
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...
 
T

Tad McClellan

Jim Gibson said:
Ben Morrow said:
Quoth Tad McClellan said:
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. :)
 
T

Tad McClellan

Fei Liu said:
$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?
 

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top