]> gitweb.factorcode.org Git - factor.git/blob - extra/money/money.factor
Updating code for make and fry changes
[factor.git] / extra / money / money.factor
1 USING: io kernel math math.functions math.parser parser lexer
2 namespaces make sequences splitting grouping combinators
3 continuations sequences.lib ;
4 IN: money
5
6 : dollars/cents ( dollars -- dollars cents )
7     100 * 100 /mod round ;
8
9 : money>string ( object -- string )
10     dollars/cents [
11         "$" %
12         swap number>string
13         <reversed> 3 group "," join <reversed> %
14         "." % number>string 2 CHAR: 0 pad-left %
15     ] "" make ;
16
17 : money. ( object -- )
18     money>string print ;
19
20 ERROR: not-a-decimal x ;
21
22 : parse-decimal ( str -- ratio )
23     "." split1
24     >r dup "-" head? [ drop t "0" ] [ f swap ] if r>
25     [ [ "0" ] when-empty ] bi@
26     dup length
27     >r [ dup string>number [ nip ] [ not-a-decimal ] if* ] bi@ r>
28     10 swap ^ / + swap [ neg ] when ;
29
30 : DECIMAL:
31     scan parse-decimal parsed ; parsing