Converts integers to ordinal strings with appropriate suffixes (1st, 2nd, 3rd, 4th, etc.).
import "github.com/dustin/go-humanize"func Ordinal(x int) stringConverts an integer to its ordinal representation by adding the appropriate suffix.
Parameters:
x - The integer to convertReturns: String with ordinal suffix (e.g., "1st", "2nd", "3rd", "4th")
Example:
humanize.Ordinal(1) // "1st"
humanize.Ordinal(2) // "2nd"
humanize.Ordinal(3) // "3rd"
humanize.Ordinal(4) // "4th"
humanize.Ordinal(11) // "11th"
humanize.Ordinal(12) // "12th"
humanize.Ordinal(13) // "13th"
humanize.Ordinal(21) // "21st"
humanize.Ordinal(22) // "22nd"
humanize.Ordinal(23) // "23rd"
humanize.Ordinal(100) // "100th"
humanize.Ordinal(101) // "101st"
humanize.Ordinal(193) // "193rd"
humanize.Ordinal(1000) // "1000th"The function follows standard English ordinal rules:
position := 3
fmt.Printf("You finished in %s place!", humanize.Ordinal(position))
// Output: "You finished in 3rd place!"
rank := 42
fmt.Printf("Your rank: %s", humanize.Ordinal(rank))
// Output: "Your rank: 42nd"year := 25
fmt.Printf("Celebrating our %s anniversary", humanize.Ordinal(year))
// Output: "Celebrating our 25th anniversary"items := []string{"Apple", "Banana", "Cherry", "Date"}
for i, item := range items {
fmt.Printf("%s item: %s\n", humanize.Ordinal(i+1), item)
}
// Output:
// 1st item: Apple
// 2nd item: Banana
// 3rd item: Cherry
// 4th item: Dateedition := 21
fmt.Printf("Welcome to the %s Annual Conference", humanize.Ordinal(edition))
// Output: "Welcome to the 21st Annual Conference"grade := 9
fmt.Printf("Student is in %s grade", humanize.Ordinal(grade))
// Output: "Student is in 9th grade"The function works with negative numbers as well:
humanize.Ordinal(-1) // "-1st"
humanize.Ordinal(-2) // "-2nd"
humanize.Ordinal(-11) // "-11th"humanize.Ordinal(0) // "0th"