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
+18.7k Golang : Check whether a network interface is up on your machine
+5.7k Golang : Denco multiplexer example
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+12.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+15.1k Golang : ROT47 (Caesar cipher by 47 characters) example
+15.6k Golang : Read large file with bufio.Scanner cause token too long error
+13.9k Golang : syscall.Socket example
+17.9k Golang : Get command line arguments
+12.4k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+17k Golang : When to use init() function?
+5.8k PHP : How to check if an array is empty ?
+23.7k Golang : Call function from another package