2

In Swift, I can do something like this:

let ordinalFormatter = NumberFormatter()
ordinalFormatter.numberStyle = .ordinal

print(ordinalFormatter.string(from: NSNumber(value: 3))) // 3rd

but I don't see any way to do this so easily in Kotlin. Is there such a way, or will I have to use 3rd-party libraries or write my own?

Ky.
  • 28,753
  • 48
  • 180
  • 285
  • 1
    Well, it is usually hard to prove that something doesn't exist. :) But I'm almost sure there's no such function in stdlib or anything that can be immediately adapted for it. Moreover, stdlib doesn't contain anything locale-specific, and you should actually resort to some third-party software or implement your own solution. – hotkey Jan 21 '17 at 00:01
  • @hotkey Sounds like an answer to me! – Ky. Jan 21 '17 at 00:05
  • 1
    Okay then, posted this as an answer. :) – hotkey Jan 21 '17 at 00:39

1 Answers1

10

Well, it is usually hard to prove that something doesn't exist. But I have never come across any function in kotlin-stdlib that would do this or could be immediately adapted for this. Moreover, kotlin-stdlib doesn't seem to contain anything locale-specific (which number ordinals certainly are).

I guess you should actually resort to some third-party software or implement your own solution, which might be something as simple as this:

fun ordinalOf(i: Int) = "$i" + if (i % 100 in 11..13) "th" else when (i % 10) {
    1 -> "st"
    2 -> "nd"
    3 -> "rd"
    else -> "th"
}

Also, solutions in Java: (here)

Community
  • 1
  • 1
hotkey
  • 127,445
  • 33
  • 333
  • 310