add money library
parent
8db19c2ee5
commit
0699bacf86
|
@ -0,0 +1,2 @@
|
||||||
|
Doug Coleman
|
||||||
|
Slava Pestov
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
Utility for calculating money with rationals
|
Loading…
Reference in New Issue