Is there a way in Java to convert an integer to its ordinal name?

后端 未结 12 1335
生来不讨喜
生来不讨喜 2020-11-27 15:12

I want to take an integer and get its ordinal, i.e.:

1 -> \"First\"
2 -> \"Second\"
3 -> \"Third\"
...
12条回答
  •  无人及你
    2020-11-27 15:48

    In Scala for a change,

    List(1, 2, 3, 4, 5, 10, 11, 12, 13, 14 , 19, 20, 23, 33, 100, 113, 123, 101, 1001, 1011, 1013, 10011) map {
        case a if (a % 10) == 1 && (a % 100) != 11 => a + "-st"
        case b if (b % 10) == 2 && (b % 100) != 12 => b + "-nd"
        case c if (c % 10) == 3 && (c % 100) != 13 => c + "-rd"
        case e                                     => e + "-th"
      }  foreach println
    

提交回复
热议问题