or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

byte-sizes.mdenglish.mdindex.mdnumber-formatting.mdordinals.mdsi-notation.mdtime-formatting.md
tile.json

ordinals.mddocs/

Ordinal Numbers

Converts integers to ordinal strings with appropriate suffixes (1st, 2nd, 3rd, 4th, etc.).

Import

import "github.com/dustin/go-humanize"

Function

Ordinal

func Ordinal(x int) string

Converts an integer to its ordinal representation by adding the appropriate suffix.

Parameters:

  • x - The integer to convert

Returns: 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"

Ordinal Rules

The function follows standard English ordinal rules:

  • Numbers ending in 1 use "st" (1st, 21st, 31st, etc.)
    • Exception: Numbers ending in 11 use "th" (11th, 111th, etc.)
  • Numbers ending in 2 use "nd" (2nd, 22nd, 32nd, etc.)
    • Exception: Numbers ending in 12 use "th" (12th, 112th, etc.)
  • Numbers ending in 3 use "rd" (3rd, 23rd, 33rd, etc.)
    • Exception: Numbers ending in 13 use "th" (13th, 113th, etc.)
  • All other numbers use "th" (4th, 5th, 6th, 7th, 8th, 9th, 10th, etc.)

Common Use Cases

Rankings and Positions

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"

Dates and Anniversaries

year := 25
fmt.Printf("Celebrating our %s anniversary", humanize.Ordinal(year))
// Output: "Celebrating our 25th anniversary"

Lists and Sequences

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: Date

Event Numbering

edition := 21
fmt.Printf("Welcome to the %s Annual Conference", humanize.Ordinal(edition))
// Output: "Welcome to the 21st Annual Conference"

Educational Context

grade := 9
fmt.Printf("Student is in %s grade", humanize.Ordinal(grade))
// Output: "Student is in 9th grade"

Handling Negative Numbers

The function works with negative numbers as well:

humanize.Ordinal(-1)   // "-1st"
humanize.Ordinal(-2)   // "-2nd"
humanize.Ordinal(-11)  // "-11th"

Zero

humanize.Ordinal(0)    // "0th"

Notes

  • The function only adds the suffix; it does not spell out the number (e.g., it returns "1st", not "first")
  • For spelled-out ordinals (e.g., "first", "second", "third"), you would need a different function or library
  • The function handles all integers including negative numbers and zero