Skip to content

Instantly share code, notes, and snippets.

@digilist
Last active April 18, 2017 13:51
Show Gist options
  • Save digilist/3c93b02eaf936cbe2ce9a6f45cf0c21c to your computer and use it in GitHub Desktop.
Save digilist/3c93b02eaf936cbe2ce9a6f45cf0c21c to your computer and use it in GitHub Desktop.
-- http://www.numbers2words.com/learn_more.php/?selected=dk
bigNnumbers :: Int -> (String, String)
bigNnumbers 1 = ("et tusind", "tusinde")
bigNnumbers 2 = ("en million", "millioner")
bigNnumbers 3 = ("en milliard", "milliarder")
bigNnumbers 4 = ("en billion", "billioner")
bigNnumbers 5 = ("en billiard", "billiarder")
pronounceNumber :: Int -> String
pronounceNumber 0 = "nul"
pronounceNumber 1 = "en"
pronounceNumber 2 = "to"
pronounceNumber 3 = "tre"
pronounceNumber 4 = "fire"
pronounceNumber 5 = "fem"
pronounceNumber 6 = "seks"
pronounceNumber 7 = "syv"
pronounceNumber 8 = "otte"
pronounceNumber 9 = "ni"
pronounceNumber 10 = "ti"
pronounceNumber 11 = "elleve"
pronounceNumber 12 = "tolv"
pronounceNumber 13 = "tretten"
pronounceNumber 14 = "fjorten"
pronounceNumber 15 = "femten"
pronounceNumber 16 = "seksten"
pronounceNumber 17 = "sytten"
pronounceNumber 18 = "atten"
pronounceNumber 19 = "nitten"
pronounceNumber 20 = "tyve"
pronounceNumber 30 = "tredive"
pronounceNumber 40 = "fyrre"
pronounceNumber 50 = "halvtreds"
pronounceNumber 60 = "tres"
pronounceNumber 70 = "halvfjerds"
pronounceNumber 80 = "firs"
pronounceNumber 90 = "halvfems"
pronounceNumber 100 = "et hundrede"
-- 21 - 99
pronounceNumber n | n > 20 && n < 100 =
(pronounceNumber (mod n 10))
++ " og "
++ (pronounceNumber ((div n 10) * 10))
-- 101 - 999
pronounceNumber n | n > 100 && n < 1000 =
(pronounceNumber (div n 100))
++ " hundrede"
++ (appendNumber "" (mod n 100))
-- > 1000
pronounceNumber n | n > 1000 =
(bigNnumber power (div n divisor))
++ (appendNumber " og " (mod n divisor))
where power = (floor (log (fromIntegral n) / (log 1000)));
divisor = 1000 ^ power
-- Negative numbers
pronounceNumber n | n < 0
= "minus " ++ (pronounceNumber (n * (-1)))
-- append a number but only if it is not 0
appendNumber :: String -> Int -> String
appendNumber word 0 = ""
appendNumber "" n = " " ++ (pronounceNumber n)
appendNumber word n = word ++ (pronounceNumber n)
-- handle the case of a
bigNnumber power 1 = fst(bigNnumbers power)
bigNnumber power n = (pronounceNumber n) ++ " " ++ (snd (bigNnumbers power))
-- return the number and the number as text
convertNumber :: Int -> String
convertNumber n = do
(show n) ++ ": " ++ pronounceNumber n
main = do
print (convertNumber 21)
print (convertNumber 8100)
print (convertNumber 9000)
print (convertNumber 1000111)
print (convertNumber 1001001)
print (convertNumber 2429870)
print (convertNumber 14000000)
print (convertNumber 123429870)
print (convertNumber 2232429870)
print (convertNumber 1223429870)
print (convertNumber 99999999999999)
-- print (convertNumber 99999999999999999)
print (convertNumber (-9000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment