DEV Community

Discussion on: Daily Challenge #296 - Years to Centuries

Collapse
 
cipharius profile image
Valts Liepiņš

Solution in Haskell:

import Numeric.Natural (Natural)

toCentury :: Natural -> String
toCentury = suffix . ceiling . (/ 100) . (+ 1) . fromIntegral
  where
    suffix x
     | x `mod` 10 == 1 = show x <> "st"
     | x `mod` 10 == 2 = show x <> "nd"
     | x `mod` 10 == 3 = show x <> "rd"
     | otherwise       = show x <> "th"