Skip to content

Instantly share code, notes, and snippets.

@jchannon
Last active January 1, 2023 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchannon/79a8f26b1fdfda4621b1ab4df7b47b7d to your computer and use it in GitHub Desktop.
Save jchannon/79a8f26b1fdfda4621b1ab4df7b47b7d to your computer and use it in GitHub Desktop.
type System.Int32 with
member x.DisplayWithSuffix() =
let f = x.ToString()
let defaultVal = f + "th"
let mutable result = ""
result <- if f.EndsWith("11") then f + "th" else defaultVal
result <- if f.EndsWith("12") then f + "th" else defaultVal
result <- if f.EndsWith("13") then f + "th" else defaultVal
result <- if f.EndsWith("1") then f + "st" else defaultVal
result <- if f.EndsWith("2") then f + "nd" else defaultVal
result <- if f.EndsWith("3") then f + "rd" else defaultVal
result
@ijrussell
Copy link

ijrussell commented Jan 1, 2023

let (|EndsWith|_|) (ending:string) (input:string) =
    if input.EndsWith(ending) then Some () else None

type System.Int32 with
    member x.DisplayWithSuffix() =
        match x.ToString() with
        | EndsWith "11" | EndsWith "12" | EndsWith "13" -> "th"
        | EndsWith "1" -> "st"
        | EndsWith "2" -> "nd"
        | EndsWith "3" -> "rd"
        | _ -> "th"
        |> fun suffix -> $"{x.ToString()}{suffix}" 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment