keyongtech


  keyongtech > perl > 12/2006

 #1  
12-14-06, 10:32 PM
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,
 #2  
12-14-06, 10:44 PM
John Bokma
"Fei Liu" <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.
 #3  
12-14-06, 10:46 PM
usenet
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
 #4  
12-14-06, 10:49 PM
Fei Liu
John Bokma wrote:
> "Fei Liu" <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.
 #5  
12-14-06, 10:59 PM
J. Gleixner
Fei Liu wrote:
> John Bokma wrote:
>
> 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
 #6  
12-14-06, 11:01 PM
usenet
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
 #7  
12-14-06, 11:04 PM
xhoster
usenet 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
 #8  
12-14-06, 11:05 PM
usenet
use...@DavidFilmer.com 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();
 #9  
12-14-06, 11:07 PM
John Bokma
"Fei Liu" <fei.liu> wrote:

> John Bokma wrote:
>
> 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.
 #10  
12-14-06, 11:18 PM
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
 #11  
12-14-06, 11:21 PM
Fei Liu
On Dec 14, 6:07 pm, John Bokma <j> wrote:
> "Fei Liu" <fei> wrote:
>>
>> 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.
 #12  
12-14-06, 11:23 PM
Ch Lamprecht
usenet wrote:
> use...@DavidFilmer.com 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
 #13  
12-15-06, 01:26 AM
Tad McClellan
Fei Liu <fei.liu> wrote:
>
> John Bokma wrote:
>
> 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;
 #14  
12-15-06, 01:37 AM
John Bokma
Tad McClellan <tadmc> wrote:

>>> "Fei Liu" <fei.liu> wrote:
>>>
>>> > 13.534e+10


[..]

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


nify :-D
 #15  
12-15-06, 02:48 PM
Fei Liu
On Dec 14, 8:26 pm, Tad McClellan <t> wrote:
> Fei Liu <fei> wrote:
>>
>>

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Then it is not a number!
>
> It is a string.
>> $str =~ s/\.(\d+)e[+](\d+)/ . '0' x ( - length ) /ie;

>


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

Similar Threads
Converting decimal number to hh:mm format

I have a large amount of data that I want to enter. I would like to enter it in the format of #### ( i.e. 1234) and have it automatically converted to 12:34 PM. Is there a...

converting exponential number to float number

Can anyone tell me how do i convert a string contianing exponential number to decimal/float number? Is there any built in method for doing this?

format a float number with specific number of decimal point

Hi, May I know that how to format a float number with specific number of decimal point, please? e.g :-- 123.456789 to 123.4568 123.123321 to 123.1233 Thanks Regards Oscar

Excel-Format-Number-Number-Decimal Places 0-OK still formulating?

So I am in Excel on a new spread sheet and I enter a long account number. To change it to a simple number, I go to Format then Cell on the Number tab I click Number, I...

How to convert a hex number (in a string format) into a decimal number?

Hi, friend, I have hex numbers stored in a string array, such as arr[0]="EC3A"; arr[1]="0x34AC"; ..... Now I want to convert them into decimal numbers in another array,...


All times are GMT. The time now is 07:27 AM. | Privacy Policy