Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
Two functions that will convert a given number to the English ordinal numeral. Can be handy in developing chat bot and also printing out proper sentences. The functions below can handle negative number as well.
Here you go!
package main
import (
"fmt"
"math"
"strconv"
)
func Ordinal(num int) string {
var ordinalDictionary = map[int]string{
0: "th",
1: "st",
2: "nd",
3: "rd",
4: "th",
5: "th",
6: "th",
7: "th",
8: "th",
9: "th",
}
// math.Abs() is to convert negative number to positive
floatNum := math.Abs(float64(num))
positiveNum := int(floatNum)
if ((positiveNum % 100) >= 11) && ((positiveNum % 100) <= 13) {
return "th"
}
return ordinalDictionary[positiveNum]
}
func Ordinalize(num int) string {
var ordinalDictionary = map[int]string{
0: "th",
1: "st",
2: "nd",
3: "rd",
4: "th",
5: "th",
6: "th",
7: "th",
8: "th",
9: "th",
}
// math.Abs() is to convert negative number to positive
floatNum := math.Abs(float64(num))
positiveNum := int(floatNum)
if ((positiveNum % 100) >= 11) && ((positiveNum % 100) <= 13) {
return strconv.Itoa(num) + "th"
}
return strconv.Itoa(num) + ordinalDictionary[positiveNum]
}
func main() {
// oridinaL tests
fmt.Println("1 : ", Ordinal(1))
fmt.Println("2 : ", Ordinal(2))
fmt.Println("3 : ", Ordinal(3))
fmt.Println("4 : ", Ordinal(4))
fmt.Println("5 : ", Ordinal(5))
fmt.Println("6 : ", Ordinal(6))
fmt.Println("7 : ", Ordinal(7))
fmt.Println("8 : ", Ordinal(8))
fmt.Println("9 : ", Ordinal(9))
fmt.Println("102 : ", Ordinal(102))
fmt.Println("-99 : ", Ordinal(-99))
fmt.Println("-1021 : ", Ordinal(-1021))
// oridinaLIZE tests
fmt.Println("1 : ", Ordinalize(1))
fmt.Println("2 : ", Ordinalize(2))
fmt.Println("3 : ", Ordinalize(3))
fmt.Println("4 : ", Ordinalize(4))
fmt.Println("5 : ", Ordinalize(5))
fmt.Println("6 : ", Ordinalize(6))
fmt.Println("7 : ", Ordinalize(7))
fmt.Println("8 : ", Ordinalize(8))
fmt.Println("9 : ", Ordinalize(9))
fmt.Println("102 : ", Ordinalize(102))
fmt.Println("-99 : ", Ordinalize(-99))
fmt.Println("-1021 : ", Ordinalize(-1021))
}
Output:
1 : st
2 : nd
3 : rd
4 : th
5 : th
6 : th
7 : th
8 : th
9 : th
102 : nd
-99 : th
-1021 : st
1 : 1st
2 : 2nd
3 : 3rd
4 : 4th
5 : 5th
6 : 6th
7 : 7th
8 : 8th
9 : 9th
102 : 102nd
-99 : -99th
-1021 : -1021st
See also : Golang : How to convert a number to words
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.7k Golang : List all packages and search for certain package
+6.9k Nginx : How to block user agent ?
+6k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+6.4k Golang : When to use make or new?
+8.7k Golang : Get SPF and DMARC from email headers to fight spam
+15.4k Golang : Get digits from integer before and after given position example
+22.6k Golang : Test file read write permission example
+7.1k Golang : Scanf function weird error in Windows
+21k Curl usage examples with Golang
+36.1k Golang : Convert(cast) int64 to string
+9.4k Golang : Validate IPv6 example
+5.6k Golang : Find change in a combination of coins example