Spelling of ordinal numbers: Difference between revisions

→‎{{header|Haskell}}: added Haskell solution
(→‎{{header|Haskell}}: added Haskell solution)
Line 615:
twenty-three thousand four hundred fifty-sixth
eight quadrillion seven trillion six billion five million four thousand third</pre>
 
=={{header|Haskell}}==
Uses solution of [[Number_names#Haskell]]
 
<lang haskell>spellOrdinal :: Integer -> String
spellOrdinal n
| n <= 0 = "not ordinal"
| n < 20 = small n
| n < 100 = case divMod n 10 of
(k, 0) -> spellInteger (10*k) ++ "th"
(k, m) -> spellInteger (10*k) ++ "-" ++ spellOrdinal m
| n < 1000 = case divMod n 100 of
(k, 0) -> spellInteger (100*k) ++ "th"
(k, m) -> spellInteger (100*k) ++ " and " ++ spellOrdinal m
| otherwise = case divMod n 1000 of
(k, 0) -> spellInteger (100*k) ++ "th"
(k, m) -> spellInteger (k*1000) ++ s ++ spellOrdinal m
where s = if m < 100 then " and " else ", "
where
small = ([ undefined, "first", "second", "third", "fourth", "fifth"
, "sixth", "seventh", "eighth", "nineth", "tenth", "eleventh"
, "twelveth", "thirteenth", "fourteenth", "fifteenth", "sixteenth"
, "seventeenth", "eighteenth", "nineteenth"] !!) . fromEnum</lang>
 
Testing
 
<lang haskell>main = mapM_ (\n -> putStrLn $ show n ++ "\t" ++ spellOrdinal n)
[1, 2, 3, 4, 5, 11, 65, 100, 101, 272, 23456, 8007006005004003]</lang>
 
<pre>λ> main
1 first
2 second
3 third
4 fourth
5 fifth
11 eleventh
65 sixty-fifth
100 one hundredth
101 one hundred and first
272 two hundred and seventy-second
23456 twenty-three thousand, four hundred and fifty-sixth
8007006005004003 eight quadrillion, seven trillion, six billion, five million, four thousand and third</pre>
 
=={{header|Java}}==
Anonymous user