Output file with string blanks and float [message #90874] |
Sun, 03 May 2015 01:26  |
Miguel
Messages: 18 Registered: April 2015
|
Junior Member |
|
|
Hi,
I'm trying to create an output file with these specific conditions :
1) If the result is negative, the corresponding output is a blank
2) If the result is ok, the output is a number with a specific number of decimal
For example, I have this initial output
-1 2.312 54.124
547.5460 -1.0 65.321
And I want
2.31 54.12
547.55 65.32
For the blank, I convert each line into a string array and I put "String(Replicate(32B, 8))" to create a blank with specific width.
However, if I convert into a string array, I cannot use the format F[w].[d] to specify the number of decimal.
I thought I could use STRNSIGNIF but the number of decimal won't be the same for each numbers.
Do you have an idea to have the output file with these two conditions ?
Is this possible to create a blank in float and not in string format ?
Thanks
Miguel
|
|
|
|
|
|
|
|
Re: Output file with string blanks and float [message #90881 is a reply to message #90880] |
Mon, 04 May 2015 06:47   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
Instead of writing a space, simply advance the file pointer to the right.
Take this example
;A line of data
data = [1.0, -1.0, 6.0]
;Search for and remove negative numbers
ipos = where(data ge 0, npos, COMPLEMENT=ineg, NCOMPLEMENT=nneg)
data = data[ipos]
;Create a format code with floats for positive numbers and spaces for negative numbers
fmt = strarr(npos + nneg)
if npos gt 0 then fmt[ipos] = 'f8.2'
if nneg gt 0 then fmt[ineg] = '8x'
fmt = '(' + strjoin(fmt, ',') + ')'
;Results
help, string(data, FORMAT=fmt)
<Expression> STRING = ' 1.00 6.00'
|
|
|
|