Skip to main content
Engineering LibreTexts

10.3: Integer Format Specifier

  • Page ID
    54287
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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”.


    This page titled 10.3: Integer Format Specifier is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.