-! Copyright (c) 2007, 2008 Aaron Schaefer.
+! Copyright (c) 2007, 2008, 2018 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
-USING: combinators.short-circuit grouping kernel math
-math.functions math.parser math.text.utils namespaces sequences
+USING: combinators combinators.short-circuit kernel math
+math.order math.parser math.text.utils namespaces sequences
splitting ;
IN: math.text.english
[ string>number number>text ]
[ [ "negative " prepend ] when ] bi*
] [
- [ CHAR: 0 - small-numbers ] { } map-as " " join
+ [ CHAR: 0 - small-numbers ] { } map-as join-words
] bi* " point " glue ;
M: complex number>text
[ 0 < " minus " " plus " ? ]
[ abs number>text " i" append ] bi
] bi* 3append ;
+
+: ordinal-suffix ( n -- suffix )
+ abs dup 100 mod 11 13 between? [ drop "th" ] [
+ 10 mod {
+ { 1 [ "st" ] }
+ { 2 [ "nd" ] }
+ { 3 [ "rd" ] }
+ [ drop "th" ]
+ } case
+ ] if ;
+
+: number-ap-style ( n -- str )
+ dup { [ integer? ] [ 0 9 between? ] } 1&&
+ [ number>text ] [ number>string ] if ;
+
+: ordinal-ap-style ( n -- str )
+ dup {
+ f "first" "second" "third" "fourth" "fifth" "sixth"
+ "seventh" "eighth" "ninth"
+ } ?nth [ nip ] [
+ [ number>string ] [ ordinal-suffix ] bi append
+ ] if* ;