Re: number format in structure [message #45662] |
Mon, 26 September 2005 11:34 |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
<kl_tah@hotmail.com> wrote in message
news:1127758210.699607.161150@g47g2000cwa.googlegroups.com.. .
> Hi Paolo,
>
> Thanks for the tip. It works for positive numbers. Do you happen to
> know how to get it to work for negative numbers?
> I get:
>
> IDL> help,a
> A FLOAT = -4.35660
> IDL> print,string(a)
> -4.35660
> IDL> print,string(a,format='(4.2)')
> ****
you need to specify enough "digits" (you also forget an 'f' in your format).
try
print,string(-4.35660,format='(f5.2)')
-4.36
Note that there are 5 characters in the printout, including the negative
sign and the decimal.
Cheers,
bob
PS the **** indicates that the number does not fit into the defined format
statement.
The answer is usually to make the format bigger.
|
|
|
Re: number format in structure [message #45663 is a reply to message #45662] |
Mon, 26 September 2005 11:10  |
Koh Leong Tah
Messages: 11 Registered: May 2005
|
Junior Member |
|
|
Hi Paolo,
Thanks for the tip. It works for positive numbers. Do you happen to
know how to get it to work for negative numbers?
I get:
IDL> help,a
A FLOAT = -4.35660
IDL> print,string(a)
-4.35660
IDL> print,string(a,format='(4.2)')
****
Thanks,
KL
> The command "string" does that for you
> a=3.55677
> b=string(a,format='(F4.2)')
> print,b
>
> 3.56 (rounded, not truncated)
>
> Now you can loop over your values, appending
> the new values and the separators you need using the
> + operator for strings (e.g. output=output+b+'&' )
>
> Cheers,
> Paolo
>>
>>
>> ?
>>
>> Thanks,
>> KL
>>
|
|
|
Re: number format in structure [message #45667 is a reply to message #45663] |
Mon, 26 September 2005 05:04  |
Paolo Grigis
Messages: 171 Registered: December 2003
|
Senior Member |
|
|
kl_tah@hotmail.com wrote:
> Hi David, Craig,
>
> Thanks for the tips.
> However, what I was really wanting to do was to output a long string of
> numbers, each entry with a different format, to a file (which will
> ultimately be used in a tex table).
> So I have, for eg.
>
> line=[strtrim(3.45,2),'&',strtrim(5.678,2),'\\'] ;and lots more
> printf,lun,line
>
> This would probably mean appending a long string for formats to printf.
> If I could get each entry in to a format I want before putting them in
> an array this would really save on a lot of counting. Perhaps there's a
> way to write a program that does something like
>
> a=3.55677
> a=format(a,'(F4.2)')
> print,a
> 3.55
The command "string" does that for you
a=3.55677
b=string(a,format='(F4.2)')
print,b
3.56 (rounded, not truncated)
Now you can loop over your values, appending
the new values and the separators you need using the
+ operator for strings (e.g. output=output+b+'&' )
Cheers,
Paolo
>
>
> ?
>
> Thanks,
> KL
>
|
|
|
Re: number format in structure [message #45668 is a reply to message #45667] |
Mon, 26 September 2005 03:07  |
peter.albert@gmx.de
Messages: 108 Registered: July 2005
|
Senior Member |
|
|
Hi,
if it's just for output purpose and you don't plan to do any subsequent
calculations, you could store all data into a string array and format
its contents properly when filling the array up with data. But I doubt
that that really helps, somewhere along the line you will have all the
"format" statements, be it here or later together with the "print"
statement.
Regards,
Peter
|
|
|
Re: number format in structure [message #45670 is a reply to message #45668] |
Sun, 25 September 2005 22:06  |
Koh Leong Tah
Messages: 11 Registered: May 2005
|
Junior Member |
|
|
Hi David, Craig,
Thanks for the tips.
However, what I was really wanting to do was to output a long string of
numbers, each entry with a different format, to a file (which will
ultimately be used in a tex table).
So I have, for eg.
line=[strtrim(3.45,2),'&',strtrim(5.678,2),'\\'] ;and lots more
printf,lun,line
This would probably mean appending a long string for formats to printf.
If I could get each entry in to a format I want before putting them in
an array this would really save on a lot of counting. Perhaps there's a
way to write a program that does something like
a=3.55677
a=format(a,'(F4.2)')
print,a
3.55
?
Thanks,
KL
|
|
|
Re: number format in structure [message #45671 is a reply to message #45670] |
Sun, 25 September 2005 19:51  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
kl_tah@hotmail.com writes:
> Hi All,
>
> does anyone happen to know how I can specify the format to which
> numbers stored in structure tags should adhere?
>
> for example, I'd like to (hypothetically) be able to do
>
> s={a:3.2f,b=2.3f}
>
> s.a=123.12345678
>
> will yield
>
> print,s.a
> 123.12
You could store the format in the structure, as in,
s = {a:0., a_format: '3.2F', $
b:0., b_format: '5.3F' }
and
print, s.a, format=s.a_format
This is still not easy if you want to make a compound print statement
with many fields.
Also, beware that a single precision floating point number can't store
the 11 significant figures of the number you entered. You would have
to use double precision (single = 6-7 decimal digits; double = 17
decimal digits).
Good luck,
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: number format in structure [message #45672 is a reply to message #45671] |
Sun, 25 September 2005 19:01  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
kl_tah@hotmail.com writes:
> does anyone happen to know how I can specify the format to which
> numbers stored in structure tags should adhere?
>
> for example, I'd like to (hypothetically) be able to do
>
> s={a:3.2f,b=2.3f}
>
> s.a=123.12345678
>
> will yield
>
> print,s.a
> 123.12
I think I would just use the FORMAT keyword on the PRINT
statement, where it make a *lot* more sense:
print, s.a, format='(F6.2)'
123.12
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|