- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a ifort flag so that leading zeros will not be printed for output
example 0.1234 vs .1234
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a ifort flag so that leading zeros will not be printed for output
example 0.1234 vs .1234
If you are using list-directed IO with default ( "*" ) formatting, no. You will need to use an edit descriptor to control the format.
The default rules for list-directed sequential write statement is documented in the online docs:
IVF User Guide
Language Reference
Data Transfer I/O Statements
WRITE Statements
Forms for Sequential WRITE Statements
Rules for List-Directed Sequential WRITE Statements
ron
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a ifort flag so that leading zeros will not be printed for output
example 0.1234 vs .1234
for example
[cpp]real(8) a a=0.1234 write(*,2) a 2 format(/,'a=',2x,f5.4,/)[/cpp]
This gives the following output
a= .1234
Is this what you are looking for?
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've run into the same problem where I need the leading zeros removed due to output having to be in a fixed format.
Unfortunately the length/size of the numbers vary, so I can't use the example Peter wrote. I have also tried reading through the online documentation, including what Ron had mentioned in his post, but nothing in there leads me to a format descriptor that can remove a leading zero unless I know the length of the numberand justchop off the first digit.
Any suggestions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you know that the values are always less than 1 and you want to suppress the leading zero, use an F format with the d value of w-1. This will also assume that all numbers are positive.
Otherwise, there is no special format or compiler option that will suppress the leading zero for values less than 1 in F format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately the numbers can range from 0 to the +/- thousands. Thank you though.
I suppose I'll have to look into writing the numbers to a variable first, check the first digit for a - or 0 and replace it with a blank, then write to the output. Or something to that effect as I imagine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Then do an internal write to a character variable then left shift the 0(s) out (with test for 0 as only character), then write the character variable out. Along the line of:
CHARACTER(80) :: BUFF
...
WRITE(UNIT=BUFF, '(F6.4)') VAR
ADJUSTL(BUFF)
DO WHILE(BUFF(1:1) .eq. '0')
BUFF(1:) = BUFF(2:)
END DO
IF(BUFF .EQ. ' ') BUFF = '0.0' ! whatever you want for 0
WRITE(OUNIT,'(A)') BUFF(1:LENTRIM(BUFF))
Testing to see if the number is .ge. 1.0 then choosing a format descriptor may be problematic due to potential round-up in conversion from binary to textusing the desired format.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Then do an internal write to a character variable then left shift the 0(s) out (with test for 0 as only character), then write the character variable out. Along the line of:
CHARACTER(80) :: BUFF
...
WRITE(UNIT=BUFF, '(F6.4)') VAR
ADJUSTL(BUFF)
DO WHILE(BUFF(1:1) .eq. '0')
BUFF(1:) = BUFF(2:)
END DO
IF(BUFF .EQ. ' ') BUFF = '0.0' ! whatever you want for 0
WRITE(OUNIT,'(A)') BUFF(1:LENTRIM(BUFF))
Testing to see if the number is .ge. 1.0 then choosing a format descriptor may be problematic due to potential round-up in conversion from binary to textusing the desired format.
Jim Dempsey
To cope with negative numbers if you still wish to remove the leading zeros you could
WRITE(UNIT=buff,...) ABS(var) where ... isan appropriate format
remove the leading zeros and prepend the negative sign
IF (var < 0.0) then
buff = '-'//buff
ENDIF
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Les,
Thanks for catching my oversight.
You can also handle the '-' in the character squish
[bash]CHARACTER(80) :: BUFF INTEGER :: I ... WRITE(UNIT=BUFF, '(F6.4)') VAR ADJUSTL(BUFF) IF(BUFF(1:1) .EQ. '-') THEN I = 2 ELSE I = 1 ENDIF DO WHILE(BUFF(I:I) .eq. '0') BUFF(I:) = BUFF(I+1:) END DO IF(BUFF .EQ. ' ') BUFF = '0.0' ! whatever you want for 0 WRITE(OUNIT,'(A)') BUFF(1:LENTRIM(BUFF)) [/bash]
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I fogot to add. You could add a test (possibly in Debug configuration) to test for '*', 'NaN', 'E', ... and even squish prepended zeros in exponent.
As always, run a rigorous set of test data through to verify you get what you want.
Jim

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page