DEV Community

Discussion on: Daily Challenge #296 - Years to Centuries

Collapse
 
mrwebuzb profile image
Asliddinbek Azizovich • Edited

Here is my golang solution


package main

import (
    "fmt"
)

func getSuffix(number int) string {
    if number%100 >= 11 && number%100 <= 13 {
        return "th"
    }
    switch number%10 {
        case 1:
            return "st"
        case 2:
            return "nd"
        case 3:
            return "rd"
    }

    return "th"
}

func yearToCentury(year int) string {
    century := year / 100

    return fmt.Sprintf("%d%s century", century+1, getSuffix(century+1))
}

func main() {
    tests := []int{1999, 2011, 2154, 2259, 1124, 2000, 20000}

    for _, year := range tests {
        fmt.Printf("%d --> %s\n", year, yearToCentury(year))
    } 
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
mellen profile image
Matt Ellen

what about the year 11122?

Collapse
 
mrwebuzb profile image
Asliddinbek Azizovich • Edited

I think, it will be 112nd century, because, current, 2020 year is inside of 21st century :)

Thread Thread
 
mellen profile image
Matt Ellen

It should be 112th not 112nd, because 12 gets a th

Thread Thread
 
mrwebuzb profile image
Asliddinbek Azizovich

ohhhh, it was grammatically mistake, sorry