Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer Formatting

Status
Not open for further replies.

dsadams

Technical User
Aug 28, 2009
1
US
I need a format statement that will effectively left justify an integer of an unknown length. In other words, it will print the minimum number of characters required. Right now if I execute:

i=1
write(6,'(a2,i)') 'i=',i
i=12
write(6,'(a2,i)') 'i=',i


I get the following:

i= 1
i= 12

but what I want is:

i=1
i=12

Of course for something as simple as this I could specify "i1" and "i2" respectively but if the number has an unknown content and could be either 1 or 12 then I have to use an IF statement to determine which format to use. This becomes prohibitive when multiple variables are printed on a single line. There has to be a way to do this automatically. Help!

In a related note, are there any recommendations for a VERY good reference on how to format in FORTRAN? Something with lots of examples? If not online then perhaps a good book?
 
I would do it at this way
fmtint.f90
Code:
[COLOR=#a020f0]program[/color] fmtint
  [COLOR=#2e8b57][b]integer[/b][/color] :: num_var
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: string_var

  num_var[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color]
  [COLOR=#0000ff]! convert number to string[/color]
  [COLOR=#804040][b]write[/b][/color](string_var, [COLOR=#ff00ff]'(i20)'[/color]) num_var
  [COLOR=#0000ff]! print left adjusted string (number)[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,a)'[/color]) [COLOR=#ff00ff]'i='[/color], [COLOR=#008080]adjustl[/color](string_var)

  num_var[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]12[/color]
  [COLOR=#0000ff]! convert number to string[/color]
  [COLOR=#804040][b]write[/b][/color](string_var, [COLOR=#ff00ff]'(i20)'[/color]) num_var
  [COLOR=#0000ff]! print left adjusted string (number)[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,a)'[/color]) [COLOR=#ff00ff]'i='[/color], [COLOR=#008080]adjustl[/color](string_var)  

  num_var[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]150[/color]
  [COLOR=#0000ff]! convert number to string[/color]
  [COLOR=#804040][b]write[/b][/color](string_var, [COLOR=#ff00ff]'(i20)'[/color]) num_var
  [COLOR=#0000ff]! print left adjusted string (number)[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,a)'[/color]) [COLOR=#ff00ff]'i='[/color], [COLOR=#008080]adjustl[/color](string_var)  
  
[COLOR=#a020f0]end program[/color]
Output:
Code:
$ g95 fmtint.f90 -o fmtint

$ fmtint
i=1                   
i=12                  
i=150
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top