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

Home » Public Forums » archive » Re: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY
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: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY [message #44156] Tue, 24 May 2005 08:50 Go to next message
Benjamin Hornberger is currently offline  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 #44159 is a reply to message #44156] Tue, 24 May 2005 08:07 Go to previous messageGo to next message
Vince Hradil is currently offline  Vince Hradil
Messages: 574
Registered: December 1999
Senior Member
How about strtrim()?
Re: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY [message #44188 is a reply to message #44159] Mon, 23 May 2005 06:09 Go to previous messageGo to next message
Benjamin Hornberger is currently offline  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
Re: FORMAT FLOATS TO STRINGS WITH 3 DIGITS ONLY [message #44239 is a reply to message #44156] Wed, 25 May 2005 08:14 Go to previous message
Vince Hradil is currently offline  Vince Hradil
Messages: 574
Registered: December 1999
Senior Member
I'm sorry (I make this mistake all the time) I meant "strmid()"
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: �����H�s�A�ȭp���R�Q�e�G.............
Next Topic: Adding an annotation to an iTool programmatically

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

Current Time: Fri Oct 10 07:27:45 PDT 2025

Total time taken to generate the page: 0.71987 seconds