cleanup on aisle DECIMAL:

db4
Doug Coleman 2008-02-11 16:45:38 -06:00
parent 9fe1dd871a
commit 008da8a6ba
1 changed files with 13 additions and 10 deletions

View File

@ -12,18 +12,21 @@ IN: money
"$" % "$" %
swap number>string swap number>string
<reversed> 3 group "," join <reversed> % <reversed> 3 group "," join <reversed> %
"." % number>string 2 48 pad-left % "." % number>string 2 CHAR: 0 pad-left %
] "" make print ; ] "" make print ;
TUPLE: not-a-decimal ; TUPLE: not-a-decimal ;
: DECIMAL:
scan : not-a-decimal ( -- * )
"." split dup length 1 2 between? [ T{ not-a-decimal } throw ;
T{ not-a-decimal } throw
] unless : parse-decimal ( str -- ratio )
?first2 "." split1
>r dup ?first CHAR: - = [ drop t "0" ] [ f swap ] if r> >r dup "-" head? [ drop t "0" ] [ f swap ] if r>
[ dup empty? [ drop "0" ] when ] 2apply [ dup empty? [ drop "0" ] when ] 2apply
dup length dup length
>r [ string>number dup [ T{ not-a-decimal } throw ] unless ] 2apply r> >r [ string>number dup [ not-a-decimal ] unless ] 2apply r>
10 swap ^ / + swap [ neg ] when parsed ; parsing 10 swap ^ / + swap [ neg ] when ;
: DECIMAL:
scan parse-decimal parsed ; parsing