factor/extra/money/money.factor

34 lines
890 B
Factor
Raw Normal View History

2008-06-25 04:25:08 -04:00
USING: io kernel math math.functions math.parser parser lexer
2008-09-10 23:11:40 -04:00
namespaces make sequences splitting grouping combinators
2008-10-02 18:51:08 -04:00
continuations ;
2008-02-11 17:21:59 -05:00
IN: money
SYMBOL: currency-token
CHAR: $ \ currency-token set-global
2008-02-11 17:21:59 -05:00
: dollars/cents ( dollars -- dollars cents )
100 * 100 /mod round ;
: (money>string) ( dollars cents -- string )
[ number>string ] bi@
[ <reversed> 3 group "," join <reversed> ]
[ 2 CHAR: 0 pad-head ] bi* "." glue ;
2008-09-06 14:58:14 -04:00
: money>string ( object -- string )
dollars/cents (money>string) currency-token get prefix ;
2008-09-06 14:58:14 -04:00
: money. ( object -- ) money>string print ;
2008-02-11 17:21:59 -05:00
ERROR: not-an-integer x ;
2008-02-11 17:45:38 -05:00
: parse-decimal ( str -- ratio )
"." split1
[ "-" ?head swap ] dip
2008-09-06 18:15:25 -04:00
[ [ "0" ] when-empty ] bi@
[
[ dup string>number [ nip ] [ not-an-integer ] if* ] bi@
] keep length
10^ / + swap [ neg ] when ;
2008-02-11 17:45:38 -05:00
SYNTAX: DECIMAL: scan-token parse-decimal suffix! ;