Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Leading Zeros

crkkos
Beginner
905 Views

Is there a ifort flag so that leading zeros will not be printed for output

example 0.1234 vs .1234

0 Kudos
9 Replies
Ron_Green
Moderator
905 Views
Quoting - crkkos

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
0 Kudos
Peter
Beginner
905 Views
Quoting - crkkos

Is there a ifort flag so that leading zeros will not be printed for output

example 0.1234 vs .1234

As Ron said , use format statemet
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
0 Kudos
hahnjw
Beginner
905 Views

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?

0 Kudos
Steven_L_Intel1
Employee
905 Views

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.

0 Kudos
hahnjw
Beginner
905 Views

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.

0 Kudos
jimdempseyatthecove
Honored Contributor III
905 Views

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

0 Kudos
Les_Neilson
Valued Contributor II
905 Views

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

0 Kudos
jimdempseyatthecove
Honored Contributor III
905 Views

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

0 Kudos
jimdempseyatthecove
Honored Contributor III
905 Views

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

0 Kudos
Reply