I would like to have a Fortran write statement formatted to depend on some variable. For example, I could write:
write(*,'(3f15.3,3f9.2)') x,y,z,(var(i),i=1,nvari)
where nvari = 3
. But, what if, in some cases, I actually have 4 variables (i.e. nvari = 4
). I would like to write something like this:
write(*,'(3f15.3,nvari(f9.2))') x,y,z,(var(i),i=1,nvari)
Now, nvari
can be anything and the output will work as I like. How can I make something like this work?
There are several ways to format output. To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.
The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character.
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
Format Specifiers Used in C%c :char single character. %d (%i) :int signed integer. %e (%E) :float or double exponential format. %f :float or double signed decimal.
If you are using Intel fortran, it has a proprietary extension for this -- you can include an existing variable in angle brackets to act as a specifier:
write(*,'(3f15.3,<nvari>f9.2)') x,y,z,(var(i),i=1,nvari)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With