home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l6p090.seq < prev    next >
Text File  |  1990-04-22  |  3KB  |  87 lines

  1. \ Lesson 6 Part 9 ( F-PC 3.5 Tutorial by Jack Brown )
  2.  
  3. \ Some Number formatting examples.
  4.  
  5. \ Print single number as four digit hex and preserve system base
  6. : H.     BASE @ >R 16 BASE !
  7.          0 <# # # # # #>
  8.          R> BASE !  TYPE SPACE ;
  9.  
  10. \ Print 16-bit number as binary saving preserving current BASE.
  11. : B.     BASE @ >R  2 BASE !
  12.          0 <#  # # # #  # # # #  # # # #  # # # #  #>
  13.          R> BASE !  TYPE SPACE ;
  14.  
  15. \ Print double number as dollars and cents with trailing sign.
  16. : $.   ( dn   -- )
  17.          TUCK DABS           \ Save sign take absolute value.
  18.          <#                  \ Initialize for conversion.
  19.          ROT   0<            \ Get sign and check it
  20.          IF   ASCII - HOLD   \ Put trailing sign in
  21.          ELSE ASCII + HOLD   \ output string.
  22.          THEN
  23.          # #                 \ Convert cents.
  24.          ASCII . HOLD        \ Put decimal point in output
  25.          #S                  \ Convert dollars
  26.          ASCII $ HOLD        \ Put dollar sign in output string.
  27.          #>                  \ end conversion, point to output string.
  28.          TYPE SPACE  ;       \ Display string followed by space.
  29.  
  30. COMMENT:
  31. This is a timely application of number formatting.  F-PC has a word that
  32. performs the same function as SHOWTIME below but it is called .TIME and
  33. is arrived at differently.  You might like to VIEW  .TIME and compare it
  34. with the definition of SHOWTIME below. They should both give exactly the
  35. same answers.
  36. COMMENT;
  37. \ Get DOS time in hundredths of a second as a double number dn.
  38. : HSECONDS ( -- dn )
  39.        GETTIME        \ MSDOS format, four bytes,  HHMMSShh
  40.        T>B       ;    \ dn= hundredth of seconds
  41.  
  42. \ Switch to base six
  43. :  SEX  ( -- )
  44.         6 BASE !   ;
  45.  
  46. \ Format seconds  or minutes.
  47. : :##   ( dn -- dn' )
  48.         #  ( base 10 )      \ convert the first digit base 10
  49.         SEX   # ( base 6 )  \ convert the second digit base 6
  50.         DECIMAL   ASCII :  HOLD  ;
  51.  
  52. \ Format hundredths of seconds.
  53. : .##   ( dn -- dn' )
  54.         # # ASCII . HOLD ;
  55.  
  56. \ Display current time as  HH:MM:SS.hh
  57. : SHOWTIME  ( -- )
  58.       HSECONDS   <#  .## :##   :##  #S  #>  TYPE  SPACE  ;
  59. COMMENT:
  60. Problem 6.10
  61. a) Redefine D. UD. D.R and UD.R so that 1234567. D. gives 1,234,567
  62.    Make sure the you words work for both large and small numbers!
  63. b) Repeat (a) so that, for example:  1234567. D. gives 1 234 567.
  64.    Note the trailing decimal point.
  65.  
  66. Problem 6.11
  67. Write B.R  H.R  and O.R   that take a number n  and a field width  w
  68. and then display  Binary, Hex, or Octal right justified in a field w
  69. wide while preserving the current system base.
  70.  
  71. Problem 6.12
  72. Write the word D.R$ that will display a double number dn as dollars and
  73. cents right justified in a field w wide as shown in the example below.
  74. Make sure you count the $ , and . when right justifying the output
  75. string.   D.R$ ( dn w -- )
  76.  
  77. 10234.55  12 D.R$ <enter>   $10,234.55
  78.  
  79. Problem 6.13
  80. Modify SHOWTIME so that the Hundredth's of a second are not shown and so
  81. that a 12 hour clock is used with AM or PM trailing as appropriate.
  82. Example:  21:11:48.81  should display as  9:11:49 PM
  83. COMMENT;
  84. ( Please Move to Lesson 6 Part 10 )
  85.  
  86.  
  87.