www.delorie.com/gnu/docs/gforth/gforth_138.html   search  
 
Buy GNU books!


Gforth Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.18.2 Formatted numeric output

Forth traditionally uses a technique called pictured numeric output for formatted printing of integers. In this technique, digits are extracted from the number (using the current output radix defined by base), converted to ASCII codes and appended to a string that is built in a scratch-pad area of memory (see section Implementation-defined options). Arbitrary characters can be appended to the string during the extraction process. The completed string is specified by an address and length and can be manipulated (TYPEed, copied, modified) under program control.

All of the integer output words described in the previous section (see section 5.18.1 Simple numeric output) are implemented in Gforth using pictured numeric output.

Three important things to remember about pictured numeric output:

doc-<# doc-<<# doc-# doc-#s doc-hold doc-sign doc-#> doc-#>>

doc-represent

Here are some examples of using pictured numeric output:

 
: my-u. ( u -- )
  \ Simplest use of pns.. behaves like Standard u. 
  0              \ convert to unsigned double
  <<#            \ start conversion
  #s             \ convert all digits
  #>             \ complete conversion
  TYPE SPACE     \ display, with trailing space
  #>> ;          \ release hold area

: cents-only ( u -- )
  0              \ convert to unsigned double
  <<#            \ start conversion
  # #            \ convert two least-significant digits
  #>             \ complete conversion, discard other digits
  TYPE SPACE     \ display, with trailing space
  #>> ;          \ release hold area

: dollars-and-cents ( u -- )
  0              \ convert to unsigned double
  <<#            \ start conversion
  # #            \ convert two least-significant digits
  [char] . hold  \ insert decimal point
  #s             \ convert remaining digits
  [char] $ hold  \ append currency symbol
  #>             \ complete conversion
  TYPE SPACE     \ display, with trailing space
  #>> ;          \ release hold area

: my-. ( n -- )
  \ handling negatives.. behaves like Standard .
  s>d            \ convert to signed double
  swap over dabs \ leave sign byte followed by unsigned double
  <<#            \ start conversion
  #s             \ convert all digits
  rot sign       \ get at sign byte, append "-" if needed
  #>             \ complete conversion
  TYPE SPACE     \ display, with trailing space
  #>> ;          \ release hold area

: account. ( n -- )
  \ accountants don't like minus signs, they use parentheses
  \ for negative numbers
  s>d            \ convert to signed double
  swap over dabs \ leave sign byte followed by unsigned double
  <<#            \ start conversion
  2 pick         \ get copy of sign byte
  0< IF [char] ) hold THEN \ right-most character of output
  #s             \ convert all digits
  rot            \ get at sign byte
  0< IF [char] ( hold THEN
  #>             \ complete conversion
  TYPE SPACE     \ display, with trailing space
  #>> ;          \ release hold area

Here are some examples of using these words:

 
1 my-u. 1
hex -1 my-u. decimal FFFFFFFF
1 cents-only 01
1234 cents-only 34
2 dollars-and-cents $0.02
1234 dollars-and-cents $12.34
123 my-. 123
-123 my. -123
123 account. 123
-456 account. (456)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003