Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format string for output dependent on a variable

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?

like image 222
Flux Capacitor Avatar asked Mar 26 '12 23:03

Flux Capacitor


People also ask

How do I format an output string?

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.

What is formatting string in C?

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.

What is format method in string?

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.

How strings are displayed with different formats?

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.


1 Answers

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)
like image 138
laxxy Avatar answered Oct 19 '22 12:10

laxxy