add money library

db4
Doug Coleman 2008-02-11 16:21:59 -06:00
parent 8db19c2ee5
commit 0699bacf86
4 changed files with 53 additions and 0 deletions

2
extra/money/authors.txt Normal file
View File

@ -0,0 +1,2 @@
Doug Coleman
Slava Pestov

View File

@ -0,0 +1,21 @@
USING: money parser tools.test ;
IN: temporary
[ -1/10 ] [ DECIMAL: -.1 ] unit-test
[ -1/10 ] [ DECIMAL: -0.1 ] unit-test
[ -1/10 ] [ DECIMAL: -00.10 ] unit-test
[ 0 ] [ DECIMAL: .0 ] unit-test
[ 0 ] [ DECIMAL: 0.0 ] unit-test
[ 0 ] [ DECIMAL: 0. ] unit-test
[ 0 ] [ DECIMAL: 0 ] unit-test
[ 1/10 ] [ DECIMAL: .1 ] unit-test
[ 1/10 ] [ DECIMAL: 0.1 ] unit-test
[ 1/10 ] [ DECIMAL: 00.10 ] unit-test
[ "DECIMAL: ." eval ] must-fail
[ "DECIMAL: f" eval ] must-fail
[ "DECIMAL: 0.f" eval ] must-fail
[ "DECIMAL: f.0" eval ] must-fail

29
extra/money/money.factor Normal file
View File

@ -0,0 +1,29 @@
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> %
"." % number>string 2 48 pad-left %
] "" make print ;
TUPLE: not-a-decimal ;
: DECIMAL:
scan
"." split dup length 1 2 between? [
T{ not-a-decimal } throw
] unless
?first2
>r dup ?first CHAR: - = [ drop t "0" ] [ f swap ] if r>
[ dup empty? [ drop "0" ] when ] 2apply
dup length
>r [ string>number dup [ T{ not-a-decimal } throw ] unless ] 2apply r>
10 swap ^ / + swap [ neg ] when parsed ; parsing

1
extra/money/summary.txt Normal file
View File

@ -0,0 +1 @@
Utility for calculating money with rationals