-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatNumbers.txt
94 lines (80 loc) · 1.99 KB
/
formatNumbers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
\ *********************************************************************
\ Numbers formatting examples
\ Filename: formatNumbers.txt
\ Date: 18 jan 2023
\ Updated: 23 jan 2023
\ File Version: 1.0
\ Forth: ueForth / ESP32forth
\ Copyright: Marc PETREMANN
\ Author: Marc PETREMANN
\ GNU General Public License
\ *********************************************************************
\ display n in fields of width size
: .r ( n width -- )
>r str
r> over - 0 max spaces
type
;
\ Use:
cr 12 5 .r
cr -4 5 .r
cr
\ one byte in binary format
: cBin# ( c[0..255] -- )
base @ >r
binary
<# # # # # # # # # #>
r> base !
;
\ Use:
36 cBin# type cr
$ff cBin# type cr
\ display 32 bit value in binary format
: .Bin ( n -- )
$100 /mod swap >r
$100 /mod swap >r
$100 /mod swap >r
cBin# type space
r> cBin# type space
r> cBin# type space
r> cBin# type
;
$ff .Bin cr
\ display: 00000000 00000000 00000000 11111111
$12345678 .Bin cr
\ display: 00010010 00110100 01010110 01111000
\ display n seconds in HH:MM:SS format
: :##
base @ >r
decimal # 6 base ! #
[char] : hold
r> base !
;
: HMS ( n -- )
<# :## :##
24 mod # # #>
;
120 HMS type \ display: 00:02:00
4500 HMS type \ display: 01:15:00
\ Display currencies
char . value centsSeparator
char $ value currencySign
: currSignHold ( -- )
currencySign [char] $ =
if
currencySign hold
else
currencySign hold bl hold
then
;
: toCurr ( n -- adr len )
base @ >r decimal \ save base and force to decimal base
dup >r abs \ duplicate to ret stack and set abs value
<# currSignHold # # \ concat decimals values
centsSeparator hold \ concat cents separator
#s r> sign #>
r> base ! \ restore base
;
-1002 toCurr type cr \ display: -10.02 $
-25 toCurr type cr \ display: -0.25 $
254 toCurr type cr \ display: 2.54 $