Formatted numeric output: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: add sample) |
(→{{header|Fortran}}: There are options, however.) |
||
Line 639: | Line 639: | ||
<lang fortran>INTEGER :: number = 7125 |
<lang fortran>INTEGER :: number = 7125 |
||
WRITE(*,"(I8.8)") number ! Prints 00007125</lang> |
WRITE(*,"(I8.8)") number ! Prints 00007125</lang> |
||
===On the other hand=== |
|||
One can engage in trickery via FORMAT statements, in particular the T format option. Unlike actual tab settings which on a typewriter go to a particular column following, T''n'' means go to column ''n''. |
|||
<lang Fortran> |
|||
INTEGER IV |
|||
REAL V |
|||
DATA V/7.125/ !A positive number. |
|||
IV = V !Grab the integer part. |
|||
WRITE (6,1) V,IV |
|||
1 FORMAT (F8.3,T1,I4.4) |
|||
END |
|||
</lang> |
|||
Output is |
|||
0007.125 |
|||
This would need adjustment for other sizes, but works as follows: The value part is printed (in the format system's working area) as "bbb7.125" (b's standing for spaces), then the T1 moves the finger back to column one, and the I4.4 writes out "0007", the .4 addendeum to I4 meaning print leading zeroes rather than leading spaces. It does not overwrite the subsequent ".125", and as no further output items appear the deed is done. Only later Fortran offers the addendum feature, but the Tab feature is much older. |
|||
Another approach would be to write forth a literal "000" at the start, but this is less flexible. In the absence of the .4 addendum, write the output to a character string (or equivalent), replace leading spaces by zeroes (watching out for negative numbers), and print the result. |
|||
=={{header|gnuplot}}== |
=={{header|gnuplot}}== |