comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: formatting exponential notation
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: formatting exponential notation [message #47956] Fri, 10 March 2006 08:42 Go to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
David Fanning writes:

> So, anyway, long story short, I wrote a program called
> NUMBER_FORMATTER to to this for me. For my purposes, it
> works great. After I saw this post, I thought I would make
> it available on my web page.
>
> http://www.dfanning.com/programs/number_formatter.pro

After posting this, I noticed that my algorithm wasn't
rounding properly:

IDL> number = -2.343583743e-5
IDL> Print, Number_Formatter(number, DECIMALS=4)
-2.3435e-5

So I've just put up a new version that does this
correctly.

IDL> Print, Number_Formatter(number, DECIMALS=4)
-2.3436e-5

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: formatting exponential notation [message #47957 is a reply to message #47956] Fri, 10 March 2006 08:03 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Steve.Morris@libero.it writes:

> I need a quick advice on the way to write number in the exponent format
> I want.
> For example, I want the number printed on my screen in the following
> form
> -2.3435e-5
>
> i.e. 4 digits after the point and as function of e-5
>
> Any suggestion? I have tryed to use the format='(EX.X)' but without
> much of a success .... :(

There must be some kind of convergence going on in the
Universe today. Just yesterday I got sick and tired of
always having to figure out how to format numbers into
strings for my widget programs. I just want to print
the value of the image under the cursor! But I don't
know if the "image" is a byte array, a float array, or
even one of those damn FITS files as a double array!

Thus, I can't figure out how big to make the text widget
that holds all these numbers. Yuck!

So, anyway, long story short, I wrote a program called
NUMBER_FORMATTER to to this for me. For my purposes, it
works great. After I saw this post, I thought I would make
it available on my web page.

http://www.dfanning.com/programs/number_formatter.pro

It would work something like this for this example. In
the normal case it would use the number of significant
digits in a float. But with the DECIMALS keyword, you
can specify how many decimal places you would like in the
result. It always trims the exponent of leading zeros.
So e-008 comes out e-8, which I like better.

IDL> number = -2.343583743e-5
IDL> Print, Number_Formatter(number)
-2.343584e-5
IDL> Print, Number_Formatter(number, DECIMALS=4)
-2.3435e-5

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: formatting exponential notation [message #47958 is a reply to message #47957] Fri, 10 March 2006 07:04 Go to previous messageGo to next message
Paolo Grigis is currently offline  Paolo Grigis
Messages: 171
Registered: December 2003
Senior Member
Kenneth P. Bowman wrote:
> In article <1141997588.908348.289430@j52g2000cwj.googlegroups.com>,
> "Steve.Morris@libero.it" <Steve.Morris@libero.it> wrote:
>
>
>> Hi all,
>>
>> I need a quick advice on the way to write number in the exponent format
>> I want.
>> For example, I want the number printed on my screen in the following
>> form
>> -2.3435e-5
>>
>> i.e. 4 digits after the point and as function of e-5
>>
>> Any suggestion? I have tryed to use the format='(EX.X)' but without
>> much of a success .... :(
>
>
> I think one reason that the E format works the way it does:
>
> IDL> print, -2.3435E-5, format = "(E12.4)"
> -2.3435E-05
>
> is to ensure that the exponent part of the field is always 4 digits.
> That makes it easy to produce uniformly-aligned tables of numbers.
>
Additionally

print,strlowcase('-2.3435E-05')

-2.3435e-05

now you only have to strip the zero...

Ciao,
Paolo

> If you really want the result to appear like this
>
> -2.3435e-5
>
> you could always format it yourself (base-10 logarithms come to mind).
>
> Ken Bowman
Re: formatting exponential notation [message #47960 is a reply to message #47958] Fri, 10 March 2006 06:28 Go to previous messageGo to next message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <1141997588.908348.289430@j52g2000cwj.googlegroups.com>,
"Steve.Morris@libero.it" <Steve.Morris@libero.it> wrote:

> Hi all,
>
> I need a quick advice on the way to write number in the exponent format
> I want.
> For example, I want the number printed on my screen in the following
> form
> -2.3435e-5
>
> i.e. 4 digits after the point and as function of e-5
>
> Any suggestion? I have tryed to use the format='(EX.X)' but without
> much of a success .... :(

I think one reason that the E format works the way it does:

IDL> print, -2.3435E-5, format = "(E12.4)"
-2.3435E-05

is to ensure that the exponent part of the field is always 4 digits.
That makes it easy to produce uniformly-aligned tables of numbers.

If you really want the result to appear like this

-2.3435e-5

you could always format it yourself (base-10 logarithms come to mind).

Ken Bowman
Re: formatting exponential notation [message #48088 is a reply to message #47956] Mon, 20 March 2006 07:19 Go to previous message
Steve.Morris@libero.i is currently offline  Steve.Morris@libero.i
Messages: 12
Registered: March 2006
Junior Member
it looks like work very nicely in trimming the decimals, but can you
also set the exponent format?
Like, asking to have all numbers with e.g. 4 decimals and writted as
function of e-5 ?
so, if you have like 24.53214e-6 writte 2.4532e-5
Cheers, S.
Re: formatting exponential notation [message #48089 is a reply to message #47956] Mon, 20 March 2006 07:19 Go to previous message
Steve.Morris@libero.i is currently offline  Steve.Morris@libero.i
Messages: 12
Registered: March 2006
Junior Member
it looks like work very nicely in trimming the decimals, but can you
also set the exponent format?
Like, asking to have all numbers with e.g. 4 decimals and writted as
function of e-5 ?
so, if you have like 24.53214e-6 writte 2.4532e-5
Cheers, S.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How do I reduce active memory used by program?
Next Topic: Intel iMac IDL performance

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:48:08 PDT 2025

Total time taken to generate the page: 0.20749 seconds