1 USING: io kernel math math.functions math.parser parser lexer
2 namespaces make sequences splitting grouping combinators
7 CHAR: $ \ currency-token set-global
9 : dollars/cents ( dollars -- dollars cents )
10 100 * 100 /mod round ;
12 : (money>string) ( dollars cents -- string )
14 [ <reversed> 3 group "," join <reversed> ]
15 [ 2 CHAR: 0 pad-left ] bi* "." glue ;
17 : money>string ( object -- string )
18 dollars/cents (money>string) currency-token get prefix ;
20 : money. ( object -- ) money>string print ;
22 ERROR: not-an-integer x ;
24 : parse-decimal ( str -- ratio )
26 [ "-" ?head swap ] dip
27 [ [ "0" ] when-empty ] bi@
29 [ dup string>number [ nip ] [ not-an-integer ] if* ] bi@
31 10 swap ^ / + swap [ neg ] when ;
34 scan parse-decimal parsed ; parsing