home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 6 Part 9 ( F-PC 3.5 Tutorial by Jack Brown )
-
- \ Some Number formatting examples.
-
- \ Print single number as four digit hex and preserve system base
- : H. BASE @ >R 16 BASE !
- 0 <# # # # # #>
- R> BASE ! TYPE SPACE ;
-
- \ Print 16-bit number as binary saving preserving current BASE.
- : B. BASE @ >R 2 BASE !
- 0 <# # # # # # # # # # # # # # # # # #>
- R> BASE ! TYPE SPACE ;
-
- \ Print double number as dollars and cents with trailing sign.
- : $. ( dn -- )
- TUCK DABS \ Save sign take absolute value.
- <# \ Initialize for conversion.
- ROT 0< \ Get sign and check it
- IF ASCII - HOLD \ Put trailing sign in
- ELSE ASCII + HOLD \ output string.
- THEN
- # # \ Convert cents.
- ASCII . HOLD \ Put decimal point in output
- #S \ Convert dollars
- ASCII $ HOLD \ Put dollar sign in output string.
- #> \ end conversion, point to output string.
- TYPE SPACE ; \ Display string followed by space.
-
- COMMENT:
- This is a timely application of number formatting. F-PC has a word that
- performs the same function as SHOWTIME below but it is called .TIME and
- is arrived at differently. You might like to VIEW .TIME and compare it
- with the definition of SHOWTIME below. They should both give exactly the
- same answers.
- COMMENT;
- \ Get DOS time in hundredths of a second as a double number dn.
- : HSECONDS ( -- dn )
- GETTIME \ MSDOS format, four bytes, HHMMSShh
- T>B ; \ dn= hundredth of seconds
-
- \ Switch to base six
- : SEX ( -- )
- 6 BASE ! ;
-
- \ Format seconds or minutes.
- : :## ( dn -- dn' )
- # ( base 10 ) \ convert the first digit base 10
- SEX # ( base 6 ) \ convert the second digit base 6
- DECIMAL ASCII : HOLD ;
-
- \ Format hundredths of seconds.
- : .## ( dn -- dn' )
- # # ASCII . HOLD ;
-
- \ Display current time as HH:MM:SS.hh
- : SHOWTIME ( -- )
- HSECONDS <# .## :## :## #S #> TYPE SPACE ;
- COMMENT:
- Problem 6.10
- a) Redefine D. UD. D.R and UD.R so that 1234567. D. gives 1,234,567
- Make sure the you words work for both large and small numbers!
- b) Repeat (a) so that, for example: 1234567. D. gives 1 234 567.
- Note the trailing decimal point.
-
- Problem 6.11
- Write B.R H.R and O.R that take a number n and a field width w
- and then display Binary, Hex, or Octal right justified in a field w
- wide while preserving the current system base.
-
- Problem 6.12
- Write the word D.R$ that will display a double number dn as dollars and
- cents right justified in a field w wide as shown in the example below.
- Make sure you count the $ , and . when right justifying the output
- string. D.R$ ( dn w -- )
-
- 10234.55 12 D.R$ <enter> $10,234.55
-
- Problem 6.13
- Modify SHOWTIME so that the Hundredth's of a second are not shown and so
- that a 12 hour clock is used with AM or PM trailing as appropriate.
- Example: 21:11:48.81 should display as 9:11:49 PM
- COMMENT;
- ( Please Move to Lesson 6 Part 10 )
-
-