Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!agate!violet.berkeley.edu!jerry From: jerry@violet.berkeley.edu (Jerry Berkman;217E;24804;;ZA78) Newsgroups: comp.lang.fortran Subject: Re: Left justify of integers. Message-ID: <1991Jun2.041830.14609@agate.berkeley.edu> Date: 2 Jun 91 04:18:30 GMT References: Sender: root@agate.berkeley.edu (Charlie Root) Distribution: comp Organization: University of California, Berkeley Lines: 49 In article quan@sol.surv.utas.edu.au (Stephen Quan) writes: >I'm having a slight, almost trivial problem, I cannot write >integers without them being right justified. > >eg write (*,*) 5 > > gives > > ......5 (. are leading spaces) > > >Is there some format statement (i1.1 or something) that will >solve this problem? I want a general solution, not just : > > write (*,'(i1)') 5 > >-- >quan@sol.surv.utas.edu.au (Stephen Quan) >Department of Surveying (I'm actually a computer scientist.) >University of Tasmania. >-- >quan@sol.surv.utas.edu.au (Stephen Quan) There is no easy way to do it. The simplest way I could do it is: character ctemp*40 integer rindex write(ctemp,'(i40)') ival print 8000, ctemp(rindex(ctemp,' ')+1:), 8000 format(' ...',a,'...) This uses a commonly available function, rindex(), which returns the location of the last occurrence of a substring in a string. in reverse. If its not on your system, its easy to write. Another alternative is to build the format on the fly. That's even uglier: character frmt*20 data frmt/'('' ...'',iXX,''...')'/ il = log10(float(ival))+1 write(frmt(9:10),'(i2)') il print frmt, ival This only works for positive nonzero integers and with an accurate log10(). Both methods are ugly, but I thought someone should give a solution. - Jerry Berkman, U.C. Berkeley, jerry@violet.berkeley.edu