Skip to main content

How to convert a number to its word representation with ordinal suffix in Kotlin

How to convert a number to its word representation with ordinal suffix in Kotlin.

Here is a detailed step-by-step tutorial on how to convert a number to its word representation with ordinal suffix in Kotlin:

Step 1: Create a function to convert a number to its word representation To start with, let's create a function called numberToWords that takes an integer parameter representing the number we want to convert. This function will return the word representation of the number.

fun numberToWords(number: Int): String {
// Implementation goes here
}

Step 2: Handle special cases for numbers ending with specific suffixes In English, there are some special cases for numbers ending with specific suffixes like "th", "st", "nd", and "rd". Let's handle these cases first. We can do this by checking the remainder when dividing the number by 10 and the remainder when dividing the number by 100.

fun numberToWords(number: Int): String {
val suffix = when {
number % 100 in 11..13 -> "th"
number % 10 == 1 -> "st"
number % 10 == 2 -> "nd"
number % 10 == 3 -> "rd"
else -> "th"
}
// Implementation continues...
}

Step 3: Convert the number to its word representation

Next, we need to convert the number to its word representation. We can do this by creating a map that maps each digit to its corresponding word representation. Then, we can iterate over the digits of the number and append the word representation of each digit to a result string.

fun numberToWords(number: Int): String {
val suffix = when {
number % 100 in 11..13 -> "th"
number % 10 == 1 -> "st"
number % 10 == 2 -> "nd"
number % 10 == 3 -> "rd"
else -> "th"
}

val digitToWord = mapOf(
0 to "zero",
1 to "one",
2 to "two",
3 to "three",
4 to "four",
5 to "five",
6 to "six",
7 to "seven",
8 to "eight",
9 to "nine"
)

val digits = number.toString().map { it.toString().toInt() }
val words = digits.map { digitToWord[it] }

// Implementation continues...
}

Step 4: Handle special cases for numbers ending with "teen" suffix

In English, numbers between 10 and 19 have a special suffix "teen" (e.g., thirteen, fourteen). Let's handle this special case by checking if the number is between 10 and 19 and appending the "teen" suffix to the word representation.

fun numberToWords(number: Int): String {
val suffix = when {
number % 100 in 11..13 -> "th"
number % 10 == 1 -> "st"
number % 10 == 2 -> "nd"
number % 10 == 3 -> "rd"
else -> "th"
}

val digitToWord = mapOf(
0 to "zero",
1 to "one",
2 to "two",
3 to "three",
4 to "four",
5 to "five",
6 to "six",
7 to "seven",
8 to "eight",
9 to "nine"
)

val words = when {
number in 10..19 -> listOf(digitToWord[number % 10] + "teen")
else -> number.toString().map { digitToWord[it.toString().toInt()] }
}

// Implementation continues...
}

Step 5: Join the words together and append the suffix

Finally, let's join the words together using the joinToString function and append the suffix to the result string. We can also handle the special case for number 0 separately.

fun numberToWords(number: Int): String {
val suffix = when {
number % 100 in 11..13 -> "th"
number % 10 == 1 -> "st"
number % 10 == 2 -> "nd"
number % 10 == 3 -> "rd"
else -> "th"
}

val digitToWord = mapOf(
0 to "zero",
1 to "one",
2 to "two",
3 to "three",
4 to "four",
5 to "five",
6 to "six",
7 to "seven",
8 to "eight",
9 to "nine"
)

val words = when {
number == 0 -> listOf(digitToWord[0])
number in 10..19 -> listOf(digitToWord[number % 10] + "teen")
else -> number.toString().map { digitToWord[it.toString().toInt()] }
}

val result = words.joinToString(" ")

return if (number == 0) result else result + suffix
}

Step 6: Test the function

Now that we have implemented the numberToWords function, let's test it with some example inputs.

fun main() {
println(numberToWords(0)) // Output: zeroth
println(numberToWords(1)) // Output: first
println(numberToWords(10)) // Output: tenth
println(numberToWords(11)) // Output: eleventh
println(numberToWords(21)) // Output: twenty first
println(numberToWords(100)) // Output: one hundredth
println(numberToWords(123)) // Output: one hundred twenty third
}

That's it! You have successfully converted a number to its word representation with ordinal suffix in Kotlin.