Integer data type: formatting

You can format an integer value as a string using the % operator or a format item. The formatting performed by the format operator or a format item is determined by the format string you use.

To format an integer as a string of decimal digits, use the d format string. The following program outputs the string 120:

  process
     local integer quantity initial { 120 }
  
     output "d" % quantity

You can move the decimal point in the output to the left by preceding the d with a number indicating the number of spaces to move the decimal point. A leading 0 will be inserted before the decimal point if the resulting number has no digits to the left of the decimal. The following program outputs 0.00120.

  process
     local integer quantity initial { 120 }
  
     output "5d" % quantity

You can suppress trailing zeros after the number by including the format modifier s in the format string. The following program outputs 0.0012.

  process
     local integer quantity initial { 120 }
  
     output "5sd" % quantity

You can change the radix (or base) in which the integer is expressed by using the format modifier r with a number indicating the radix to use. The following code prints an integer value in hexadecimal (base 16). The output string is fd:

  process
     local integer quantity initial { 253 }
  
     output "16rd" % quantity

You can force the values expressed in a radix greater than 10 to use uppercase letters by adding the u format modifier. The following code outputs FD

  process
     local integer quantity initial { 253 }
  
     output "16rud" % quantity

You can use the l format modifier in the same way as u to force letters to lowercase.

You can pad the output string with spaces on the right side by using the f format modifier with a number indicating the total size of the output string. The program

  process
     local integer quantity initial { 253 }
  
     output "[" || "8fd" % quantity || "]"
will output
  [253     ]
If you specify a number smaller than the number of digits in the number, all the digits will be shown. You can force the padding to occur on the left instead of the right by adding the k format modifier. The program
  process
     local integer quantity initial { 253 }
  
     output "[" || "8fkd" % quantity || "]"
will output
  [     253]

You can change the padding from blanks to zeros by inserting the z modifier instead of the k modifier. The following code outputs 00000253:

  process
     local integer quantity initial { 253 }
  
     output "8fzd" % quantity

The z modifier always pads on the left. You cannot pad with zeros on the right.

All reasonable combinations of these formatting modifiers are permitted with the d format command. For instance, the following code will output a two digit hex number using uppercase letters. The output is 0B:

  process
     local integer quantity initial { 11 }
  
     output "2f16ruzd" % quantity

Outputting binary representation of numbers

If you are outputting binary data, you will sometimes need to output a byte whose value is the value of an integer. You can do this using the b format command.

The following program outputs a byte with the value 69. Since 69 is the ASCII code for the capital letter E, the output is displayed as the letter E:

  process
     output "b" % 69

Note that output "b" % 69 has the same effect as output "%69#". The only difference is that using the format operator allows you to set the value dynamically.

Since an integer is a 32-bit number, it can hold a value representing the values of up to four bytes. You can output more than one byte based on the value of an integer by using the f format modifier with a value of 2, 3, or 4, to indicate the number of bytes to output.

In the following sample we created a large integer value and output its value as four bytes. To make the order of the output clear, we created the large value by starting with the ASCII codes of the letters E, F, G, and H and multiplying them by the appropriate values to create a large number that created a set of four bytes equivalent to the ASCII codes of the original number:

  process
     local integer E initial { 69 }
     local integer F initial { 70 * 256 }
     local integer G initial { 71 * 256 * 256 }
     local integer H initial { 72 * 256 * 256 * 256 }
     local integer four-byte-value
  
     set four-byte-value to E + F + G + H
  
     output "4fb" % four-byte-value || "%n"

If you run this program you will discover that the output is HGFE, rather than EFGH as you might have expected. This is because, by default, a multi-byte binary value is stored in big-endian fashion, with the most significant byte first. This is the appropriate order for outputting binary data on many systems.

If you need to change the order in which the bytes of a multi-byte binary value are output, you can do so by specifying a number between 0 and 3 between the f and the b. The easiest way to explain the different orderings that result is by example:

  process
     local integer E initial { 69 }
     local integer F initial { 70 * 256 }
     local integer G initial { 71 * 256 * 256 }
     local integer H initial { 72 * 256 * 256 * 256 }
     local integer four-byte-value
  
     set four-byte-value to E + F + G + H
  
     output "4f0b" % four-byte-value || "%n"
     output "4f1b" % four-byte-value || "%n"
     output "4f2b" % four-byte-value || "%n"
     output "4f3b" % four-byte-value || "%n"
This program produces the following output:
  HGFE
  GHEF
  FEHG
  EFGH

The default byte ordering command for data output with the b format command is 0.

You can change the default ordering at any time by using the binary-output declaration or by opening the output stream with the with binary modifier.

The order of precedence for these methods is:

  1. the byte order modifier on the %b format item,
  2. the ordering specified by the binary modifier on the stream to which the value is being written,
  3. the binary-output declaration, and
  4. the byte ordering default (0).

Outputting Roman numerals

You can also output integer values as Roman numerals using the i format command:

  process
     local integer year initial { 1999 }
  
     output "i" % year || "%n"

You can use the u, l, f, and k format modifiers with the format command i. They work just as with d.

Outputting a sequential alphabetic representation

You can format an integer as an alphabetic sequence like that used to number rows in a theatre (a...z, aa...az, and so forth) using the a format command:

  process
     output "a" % 9 || "%n"
         || "a" % 29 || "%n"
         || "a" % 2999 || "%n"

This program outputs:

  i
  ac
  dki

You can use the l, u, f, and k modifiers with the a format command.

You can drop the characters i, l, and o from the alphabetic sequence using the j format modifier (these characters are easily confused with numbers).

You can use the w modifier to change the alphabetic sequence generated so that numbers greater than 26 (or 23 when combined with j) are generated by repeating the same letter multiple times. Thus, the sequence runs x, y, z, aa, bb, cc, dd, ... xx, yy, zz, aaa, bbb, and so forth.

Prerequisite Concepts