factor/extra/money/money.factor

30 lines
770 B
Factor
Raw Normal View History

2008-02-11 17:21:59 -05:00
USING: io kernel math math.functions math.parser parser
2008-06-09 06:22:21 -04:00
namespaces sequences splitting grouping combinators
continuations sequences.lib ;
2008-02-11 17:21:59 -05:00
IN: money
: dollars/cents ( dollars -- dollars cents )
100 * 100 /mod round ;
: money. ( object -- )
dollars/cents
[
"$" %
swap number>string
<reversed> 3 group "," join <reversed> %
2008-02-11 17:45:38 -05:00
"." % number>string 2 CHAR: 0 pad-left %
2008-02-11 17:21:59 -05:00
] "" make print ;
2008-05-20 11:17:34 -04:00
ERROR: not-a-decimal x ;
2008-02-11 17:45:38 -05:00
: parse-decimal ( str -- ratio )
"." split1
>r dup "-" head? [ drop t "0" ] [ f swap ] if r>
2008-03-29 21:36:58 -04:00
[ dup empty? [ drop "0" ] when ] bi@
2008-02-11 17:21:59 -05:00
dup length
2008-05-20 11:17:34 -04:00
>r [ dup string>number [ nip ] [ not-a-decimal ] if* ] bi@ r>
2008-02-11 17:45:38 -05:00
10 swap ^ / + swap [ neg ] when ;
: DECIMAL:
scan parse-decimal parsed ; parsing