home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l6p090
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
114 lines
╔════════════════════════════════════════════════════╗
║ Lesson 6 Part 090 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 ;
Example:
1234 H. <enter> 04D2 ok
5555 H. <enter> 15B3 ok
1024 H. <enter> 0400 ok
\ Print 16-bit number as binary saving preserving current BASE.
: B. BASE @ >R 2 BASE !
0 <# # # # # # # # # # # # # # # # # #>
R> BASE ! TYPE SPACE ;
Example:
1234 B. <enter> 0000010011010010 ok
5555 B. <enter> 0001010110110011 ok
1024 B. <enter> 0000010000000000 ok
\ 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.
Example:
1234. $. <enter> $12.34+ ok
-1234. $. <enter> $12.34- ok
555.55 $. <enter> $555.55+ ok
-555.55 $. <enter> $555.55- ok
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.
\ 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 ;
Example:
SHOWTIME <enter> 21:11:46.23 ok \ This is our version
.TIME <enter> 21:11:48.81 ok \ This is F-PC's version.
╓──────────────╖
║ 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
┌────────────────────────────────────┐
│ Please Move to Lesson 6 Part 100 │
└────────────────────────────────────┘