Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions ordinals.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@ import "strconv"
// Ordinal gives you the input number in a rank/ordinal format.
//
// Ordinal(3) -> 3rd
// Ordinal(-1) -> -1st
func Ordinal(x int) string {
// Suffix selection must use the absolute value because Go's % for
// negative dividends yields a negative remainder (e.g. -1%10 == -1),
// which would never match the positive cases below.
n := x
if n < 0 {
n = -n
}

suffix := "th"
switch x % 10 {
switch n % 10 {
case 1:
if x%100 != 11 {
if n%100 != 11 {
suffix = "st"
}
case 2:
if x%100 != 12 {
if n%100 != 12 {
suffix = "nd"
}
case 3:
if x%100 != 13 {
if n%100 != 13 {
suffix = "rd"
}
}
Expand Down
14 changes: 14 additions & 0 deletions ordinals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,19 @@ func TestOrdinals(t *testing.T) {
{"211", Ordinal(211), "211th"},
{"212", Ordinal(212), "212th"},
{"213", Ordinal(213), "213th"},
// Negatives: Go's % keeps the dividend's sign, so suffix selection
// must use the absolute value.
{"-1", Ordinal(-1), "-1st"},
{"-2", Ordinal(-2), "-2nd"},
{"-3", Ordinal(-3), "-3rd"},
{"-4", Ordinal(-4), "-4th"},
{"-11", Ordinal(-11), "-11th"},
{"-12", Ordinal(-12), "-12th"},
{"-13", Ordinal(-13), "-13th"},
{"-21", Ordinal(-21), "-21st"},
{"-22", Ordinal(-22), "-22nd"},
{"-23", Ordinal(-23), "-23rd"},
{"-101", Ordinal(-101), "-101st"},
{"-111", Ordinal(-111), "-111th"},
}.validate(t)
}