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

write format problem ( real )

Ara_K_
Beginner
574 Views

hello, all

I write a simple program for writing real variable.

But  case1 and case 2 has some space in front of the number.

Then I tried using 0PF0.4 format, but it removed the number zero.

Finally I changed real variable to character and print it.

Is there any other way to remove the space before number with number zero?

-------------------------------------------

PROGRAM TEST
REAL :: e=0.1425
CHARACTER(LEN=10) :: rstr1

write (*,*) e                                       !case1
write (*,"(f10.4)") e                           !case2
write (*,"(0PF0.4)") e                        !case3

write (rstr1,"(0PF10.4)") e             
write(*,*) rstr1
write (*,'(a)') trim(adjustl(rstr1))        !case4

END PROGRAM TEST

-------------------------------------------

> ./a.out
  0.1425000
    0.1425
.1425
     0.1425
0.1425

 

-------------------------------------------

 
 
0 Kudos
5 Replies
JVanB
Valued Contributor II
574 Views

Try

WRITE(*,'(*(g0))') e

List-directed formatted writes always put that leading space in there just in case you are outputting to a 1960s-style IBM printer.

 

0 Kudos
mecej4
Honored Contributor III
574 Views

Repeat Offender wrote:

Try

WRITE(*,'(*(g0))') e

List-directed formatted writes always put that leading space in there just in case you are outputting to a 1960s-style IBM printer.

Perhaps we can ask a large automobile company to write us a Fortran compiler using which we can produce software that can sense when it is connected to a line-printer and output spaces only if it is.

0 Kudos
TimP
Honored Contributor III
574 Views

Way back when postscript was introduced, it was accompanied by filters (a2ps, enscript) to translate the legacy line formatting.  Those are still available, and could be adapted if you find that an easier path than explicit formats.

0 Kudos
FortranFan
Honored Contributor III
574 Views

mecej4 wrote:

..

Perhaps we can ask a large automobile company to write us a Fortran compiler using which we can produce software that can sense when it is connected to a line-printer and output spaces only if it is.

And when the company is from the most prosperous part of the old continent, the software will work perfectly when mecej4 is using it to test but said sensors will be turned off when ordinary users try to run it :-)))

0 Kudos
Ara_K_
Beginner
574 Views

Repeat Offender wrote:

Try

WRITE(*,'(*(g0))') e

List-directed formatted writes always put that leading space in there just in case you are outputting to a 1960s-style IBM printer.

 

Thanks for your kind answer.

I tried this format, but it still didn't have zero before decimal point...

------------------result---------------------

.1425000

0 Kudos
Reply