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
+29k Golang : missing Git command
+6.6k Golang : Get expvar(export variables) to work with multiplexer
+19.6k Golang : Archive directory with tar and gzip
+5.5k Golang : Detect words using using consecutive letters in a given string
+8.6k Android Studio : Image button and button example
+18.1k Golang : How to remove certain lines from a file
+5.9k PHP : Get client IP address
+6.2k Golang : Detect face in uploaded photo like GPlus
+25.4k Golang : Daemonizing a simple web server process example
+12.1k Golang : Flush and close file created by os.Create and bufio.NewWriter example