Next: , Previous: , Up: Basic Formatting   [Contents][Index]


6.4.3.2 Character and Integer Directives

FORMAT supports several directives that can be used to emit values of specific types in particular ways.

  1. Character Directives
    ~C

    used to emit characters. It takes no prefix arguments but can be modified with the colon and at-sign modifiers. Unmodified, its behavior is no different from ‘~A’ except that it works only with characters.

    ~:C

    outputs nonprinting characters such as space, tab, and newline by name. This is useful if you want to emit a message to the user about some character.

    (format t "Syntax error. Unexpected character: ~:c" char)
    ==> Syntax error. Unexpected character: Space
    
    ~:@C

    With both the colon and at-sign modifiers, the ‘~C’ directive can print extra information about how to enter the character at the keyboard if it requires special key combinations.

  2. Number Directives

    While you can use the ‘~A’ and ‘~S’ directives to emit numbers, if you want fine control over how they’re printed, you need to use one of the number-specific directives.

    The numeric directives can be divided into two subcategories: directives for formatting integer values and directives for formatting floating-point values.

    1. Integer Directives

      Five closely related directives format integer values: ‘~D’, ‘~X’, ‘~O’, ‘~B’, and ‘~R’.

      ~D

      the ~D directive outputs integers in base 10.

      ~:D

      with a colon modifier it adds commas.

      ~@D

      And with an at-sign modifier, it always prints a sign.

      ~:@D

      the two modifiers can be combined.

      ~#D

      The first prefix parameter can specify a minimum width for the output

      ~#,'#D

      the second parameter can specify a padding character to use.

      ~,,'.D

      the third parameter specifies the character to use as the separator between groups and digits (defaults to a comma)

      ~,,,4D

      the fourth parameter specifies the number of digits per group (defaults to 3)

      Thus, you can use the directive ‘~:D’ without parameters to output large integers in standard format for the United States but can change the comma to a period and the grouping from 3 to 4 with ‘~,,'.,4D’.

      ~X’ ‘~O’ ‘~B

      work just like the ‘~D’ directive except they emit numbers in hexadecimal (base 16), octal (base 8), and binary (base 2).

      (format nil "~x" 1000000) ==> "f4240"
      (format nil "~o" 1000000) ==> "3641100"
      (format nil "~b" 1000000) ==> "11110100001001000000"
      
      ~R

      the general radix directive. It is a number between 2 and 36 (inclusive) that indicates what base to use. The remaining parameters are the same as the four parameters accepted by the ~D, ~X, ~O, and ~B directives, and the colon and at-sign modifiers modify its behavior in the same way.

    2. Floating-Point Directives

      Four directives format floating-point values: ‘~F’, ‘~E’, ‘~G’, and ‘~$’. You can use the ‘~F’, ‘~E’, and ‘~$’ directives to interpolate floating-point values into text. The ‘~G’, or general, floating-point directive, on the other hand, combines aspects of the ‘~F’ and ‘~E’ directives in a way that only really makes sense for generating tabular output.

      ~F

      emits its argument, which should be a number, in decimal format, possibly controlling the number of digits after the decimal point. It is allowed to use computerized scientific notation if the number is sufficiently large or small. The second parameter controls the number of digits to print after the decimal point. Use the @-sign modifier to print a sign.

      (format nil "~f" pi)   ==> "3.141592653589793d0"
      (format nil "~,4f" pi) ==> "3.1416"
      
      ~E

      always emits numbers in computerized scientific notation. Use the @-sign modifier to print a sign.

      (format nil "~e" pi)   ==> "3.141592653589793d+0"
      (format nil "~,4e" pi) ==> "3.1416d+0"
      
      ~$

      monetary, directive is similar to ‘~F’. With no parameters, it’s basically equivalent to ‘~,2F’. To modify the number of digits printed after the decimal point, you use the first parameter, while the second parameter controls the minimum number of digits to print before the decimal point. Use the @-sign modifier to print a sign.

      (format nil "~$" pi)    ==> "3.14"
      (format nil "~2,4$" pi) ==> "0003.14"
      

Next: English Language Directives, Previous: General Directives, Up: Basic Formatting   [Contents][Index]