Click here to download the examples that go with this.
IDL uses fortran like format codes. The basic classes of format codes are I, F, E, A, and X. These all are used for displaying different types of data, and they all have a slightly different syntax. The usage for each is summarized below, and then I explain how to combine them to make actual format specifications for printing. Format codes are case-insensitive.
I
The 'i' format code is used if you want to display data as an integer. This can be used for floats as well. It simply displays the integer part of a variable, and omits the decimal point and any floating point information. The letter 'i' is followed immediately by the number of digits that should be displayed. If the number is longer than the number of digits you allow it, it will be replaced by *'s filling up the number of digits you specify. So, if you try to print the number '456' with the format code 'i2' IDL will print '**'. If you give more room for digits than the number takes up, IDL will pad it with spaces to fill the specified number of characters. Finally, the 'i0' code tells IDL to use a number's "natural" width - i.e. it will print out the number itself without any padding spaces.
F
The 'f' format code is used to display data that has a fractional value. A floating point format code needs to specify two values - the total length of the displayed number (including the decimal point and negative sign if it has one) and the number of digits to display after the decimal point. So for instance to display the number -5.23 in IDL you would use the format code 'f5.2'. The number 23.263 would use the code 'f6.3'. Again, if the number doesn't fit in the specified format statement, IDL will print out *'s to fill out the specified number of digits. For instance if you print out the number -5.23 using the format code 'f4.2' IDL will print '****'.
E
The 'e' format code is used to display data in exponential format. Similar to F, you have to tell the format code the total number of characters to use and how many characters to display after the decimal point. In this case the result will be displayed in the format: X.XXE+YY. The last four characters (E+YY) are a fixed format so your 'e' format code must always be at least 7 characters long (X.XE+YY). So the minimum format specification will be 'e7.1' which would result in something like 1.6E+15 or 8.2E-23.
A
The 'a' format code is used to display strings, and only string variables can use the a format code. The a format code is simply followed by the number of characters the variable should use up. If the string is shorter than the number of characters specified, it will be padded with spaces to fill it out. If the string is longer than the format code, then the end of the string will be chopped off to fit.
X
The 'x' format code is used to display spaces. It is used primarily to put spaces between columns of data. The x is preceded by the number of spaces to print out. For instance, '2x' would specify two spaces ' '.
Putting it together
Format codes in IDL must always be contained within parenthesis. So to print out a floating point variable using the 'f' code you would type:
print,5.234,format='(f5.3)'
When printing out arrays or multiple variables, you can specify multiple format codes by separating them with commas. Just make sure and put spaces in between.
print,5.234,28.16,format='(f5.3, 1x, i0)'
You can use additional parenthesis to group format codes and repeat them by putting a repeat index before the opening parenthesis.
print,findgen(5),format='( 5( 1x, f3.1 ) )'
A format code in IDL specifies one line of output, so if you have more variables than format codes IDL will automatically wrap the output to a new line.
print,findgen(10),format='( 5( 1x, f3.1 ) )'