10.3: Integer Format Specifier
- Page ID
- 54287
The integer format specifier rIw
or rIw.m
is used tell the system exactly how many positions should be used to either read or write an integer variable. The w
is the width or how many total places are used. If the number is negative, the sign uses a place. The m
is optional and can be used to set a minimum number of digits to display, which will display leading zeros if needed in order to display the minimum number of digits. The r
is the number of times the format specifier should be repeated.
A format of '(i6)'
would look like:
x | x | x | x | x | x |
\(\leftarrow \quad \) w \(\quad \rightarrow\) |
For example, given the declarations,
integer :: num1=42, num2=123, num3=4567
the following write statement can be used to display the value in variable num1
with no leading or trailing spaces.
write (*,'(i2)') num1
Which will display “42”.
Multiple variables can be displayed. For example, to display the values in variables num1
and num2
, with no leading or trailing spaces.
write (*,'(i2,i3)') num1, num2
Which will display “42123” with no spaces between the two different values. However,
write (*,'(i2,i4)') num1, num2
will display “42 123” with one space between the values. Further,
write (*,'(i5,i5,i5)') num1, num2, num3
will display “ 42 123 4567” where each variable uses 5 spaces. And, finally,
write (*,'(i6.4)') num1
will display “ 0042”.