Re: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY [message #44156] |
Tue, 24 May 2005 08:50  |
Benjamin Hornberger
Messages: 258 Registered: March 2004
|
Senior Member |
|
|
hradilv wrote:
> How about strtrim()?
>
You can use STRTRIM to convert a float to strıng, but if you want to
control the output format, you'll have to use STRTRIM(STRING(...,
format=...)), since STRTRIM doesn't accept the FORMAT keyword. Then you
can just use 0 as field width and save the call to STRTRIM.
But yes, in principle you can convert to a fixed width string, padded
with spaces, and then trim the spaces away.
Benjamin
|
|
|
|
Re: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY [message #44188 is a reply to message #44159] |
Mon, 23 May 2005 06:09   |
Benjamin Hornberger
Messages: 258 Registered: March 2004
|
Senior Member |
|
|
Giorgos wrote:
> Hi
>
> I have the following problem....
> I have an array of float numbers consisting of many decimal points and
> I want to convert it to string ... I used the string command but how
> can I isolate and keep only the first 3 digits from each element of the
> array? I played with the format keyword but... a mess...
> can anyone help?
>
If you want to print 3 digits after the decimal point and don't want any
leading spaces:
IDL> print, string(1.5345435435435, format='(f0.3)')
If you want only 3 significant digits and the number of digits before
the decimal point is not constant, you should go for scientific notation:
IDL> print, string(3432432.4343, format='(e0.2)')
(meaning two digits after the decimal point, making 3 significant digits)
You can also try the G format code, which uses scientific notation only
when necessary:
IDL> print, string(33432432.423423, format='(g0.3)')
(3 significant digits)
As far as I know, there is no format code which allows you to output
only X significant digits without using scientific notation at all.
The 0 in the format codes above makes the string just as long as
necessary (no leading spaces). If you want fixed width (padded with
spaces), give the number of characters you want in the string instead of
the 0 (which must include all the digits, the decimal point, the sign
and in scientific notation also the 'e').
Read the IDL help again about format codes, section "F, D, E, and G
format codes". It's quite confusing, but you'll need it all the time, so
why not tackle it now?
Good luck,
Benjamin
|
|
|
|