DEV Community

Discussion on: Daily Challenge #54 - What century is it?

Collapse
 
jay profile image
Jay • Edited

Rust:

fn century_name(year: u32) -> String {
    let century = (year / 100) + 1;
    format!(
        "{}{}",
        century,
        match century % 10 {
            _ if century > 10 && century < 14 => "th", // teen numbers exception
            1 => "st",
            2 => "nd",
            3 => "rd",
            _ => "th",
        }
    )
}