1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: grouping io kernel lexer math math.functions math.parser
4 namespaces sequences splitting ;
8 CHAR: $ currency-token set-global
10 : dollars/cents ( dollars -- dollars cents )
11 100 * 100 /mod round >integer ;
13 : format-money ( dollars cents -- string )
15 [ <reversed> 3 group "," join <reversed> ]
16 [ 2 CHAR: 0 pad-head ] bi* "." glue ;
18 : money>string ( number -- string )
19 dollars/cents format-money currency-token get prefix ;
21 : money. ( number -- ) money>string print ;
23 ERROR: not-an-integer x ;
25 : split-decimal ( str -- neg? dollars cents )
26 "." split1 [ "-" ?head swap ] dip ;
28 : parse-decimal ( str -- ratio )
29 split-decimal [ [ "0" ] when-empty ] bi@
31 [ dup string>number [ ] [ not-an-integer ] ?if ] bi@
32 ] keep length 10^ / + swap [ neg ] when ;
34 SYNTAX: DECIMAL: scan-token parse-decimal suffix! ;