Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let unitsElement(x) =
match x % 10 with
| 0 -> []
| x -> [ string x ]
let tensElement(x) =
string (x / 10 * 10)
let elementsFor2DigitNumber(x) =
match x with
| 0 -> []
| x when x < 21 -> [ string x ]
| x -> tensElement(x) :: unitsElement (x)
let elementsFor3DigitNumber(x) =
match x with
| x when x < 100 -> elementsFor2DigitNumber(x)
| _ ->
string (x / 100) :: "hundred" ::
match x % 100 with
| r when r = 0 -> []
| r -> ("and" :: elementsFor2DigitNumber(r))
let generateSeparatorText(digitGroupName, currentDigitGroup, followingDigitGroup) =
match (digitGroupName, currentDigitGroup, followingDigitGroup) with
| (d,c,f) when c % 1000 = 0 -> []
| (d,c,f) when d = "thousand" && f > 0 && f < 100 -> [ digitGroupName; "and" ]
| (d,c,f) -> [ d ]
let rec generateDigitGroupElements(x, digitGroupNames) =
match x with
| x when x < 1000 -> elementsFor3DigitNumber(x)
| x ->
match digitGroupNames with
| [] -> failwith "Shouldn't get here"
| digitGroupName :: tail ->
let quotient = x / 1000
let remainder = x % 1000
generateDigitGroupElements(quotient, tail) @ generateSeparatorText(digitGroupName, quotient % 1000, remainder) @ elementsFor3DigitNumber(remainder)
let generateElements(x) =
match x with
| x when x < 0 -> raise <| System.ArgumentOutOfRangeException()
| x when x = 0 -> [ "0" ]
| x -> (generateDigitGroupElements(x, [ "thousand"; "million"; "billion" ]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.