Skip to main content
Engineering LibreTexts

10.4: Real Format Specifier

  • Page ID
    54288
  • \( \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 real format specifier rFw.d is used tell the system exactly how many positions should be used to either read or write a real variable. The w is the width or how many total places are used, including the decimal point. If the number is negative, the sign uses a place. The d is how digits are displayed after the decimal point, which does not count the decimal point. The r is the number of times the format specifier should be repeated.

    A format of '(f6.2)' would look like:

    x x x . x x
      \(\leftarrow\) d \(\rightarrow\)
    \(\leftarrow \quad\) w \(\quad \rightarrow\)

    For example, given the declarations,

    real :: var1=4.5, var2=12.0, var3=2145.5713
    

    the following write statement can be used to display the value in variable var1 with no leading or trailing spaces.

    write (*,'(f3.1)') var1
    

    Which will display “4.5” with no leading spaces. Multiple variables can be displayed. For example, to display the values in variables var1 and var2.

    write (*,'(f5.2,f8.3)') var1, var2
    

    Which will display “ 4.50 12.000”. Another example with three variables, var1, var2, and var3, is as follows:

    write (*,'(f10.4,f10.4,f10.4)') var1, var2, var3
    

    Which will display “ 4.5000 12.0000 2145.5713” where each variable uses 10 spaces with each having exactly 4 digits after the decimal point.

    Although we may print a number using as many positions as you want, this is only for input/output formatting. The number of positions or size is not the precision (i.e., the number of significant digits) of that number. The default precision of real numbers is about seven significant digits. This is the precision of real numbers. However, we can print a real number using 50 positions in which 25 positions are for the fractional part. This is only a way of describing the appearance and does not change the precision of real numbers.


    This page titled 10.4: Real 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.