factor/extra/money/money.factor

33 lines
811 B
Factor
Raw Normal View History

2008-02-11 17:21:59 -05:00
USING: io kernel math math.functions math.parser parser
namespaces sequences splitting combinators continuations
sequences.lib ;
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 ;
TUPLE: not-a-decimal ;
2008-02-11 17:45:38 -05:00
: not-a-decimal ( -- * )
T{ not-a-decimal } throw ;
: 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-03-29 21:36:58 -04:00
>r [ string>number dup [ not-a-decimal ] unless ] bi@ r>
2008-02-11 17:45:38 -05:00
10 swap ^ / + swap [ neg ] when ;
: DECIMAL:
scan parse-decimal parsed ; parsing