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
+28.6k Golang : Saving(serializing) and reading file with GOB
+7.8k Golang : Auto-generate reply email with text/template package
+25.3k Golang : How to write CSV data to file
+5.5k Javascript : How to replace HTML inside <div>?
+10.7k Golang : Intercept and process UNIX signals example
+3.8k Javascript : Empty an array example
+9.6k Golang : Get escape characters \u form from unicode characters
+36k Golang : Convert(cast) int64 to string
+7.2k Javascript : Push notifications to browser with Push.js
+7.5k Golang : Regular Expression find string example
+7.1k Golang : Handling Yes No Quit query input
+10.8k Golang : How to pipe input data to executing child process?