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
+14.5k Golang : GUI with Qt and OpenCV to capture image from camera
+4.9k Golang : Constant and variable names in native language
+39k Golang : How to read CSV file
+27.4k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+8.9k Golang : Build and compile multiple source files
+7.4k Golang : Detect sample rate, channels or latency with PortAudio
+14k Javascript : Prompt confirmation before exit
+15.5k Golang : Validate hostname
+17.6k Convert JSON to CSV in Golang
+5.6k Unix/Linux/MacOSx : Get local IP address
+15.6k Golang : Get checkbox or extract multipart form data value example
+17.6k How to enable MariaDB/MySQL logs ?