How to convert a number to its English word representation in Kotlin
How to convert a number to its English word representation in Kotlin.
Here's a detailed step-by-step tutorial on how to convert a number to its English word representation in Kotlin:
Step 1: Define a function to handle single-digit numbers
First, let's define a function that can convert a single-digit number to its English word representation. We'll call this function convertSingleDigit.
private fun convertSingleDigit(num: Int): String {
return when (num) {
0 -> "zero"
1 -> "one"
2 -> "two"
3 -> "three"
4 -> "four"
5 -> "five"
6 -> "six"
7 -> "seven"
8 -> "eight"
9 -> "nine"
else -> throw IllegalArgumentException("Invalid input: $num")
}
}
Step 2: Define a function to handle two-digit numbers
Next, let's define a function that can convert a two-digit number to its English word representation. We'll call this function convertTwoDigits.
private fun convertTwoDigits(num: Int): String {
if (num < 10) {
return convertSingleDigit(num)
} else if (num < 20) {
return when (num) {
10 -> "ten"
11 -> "eleven"
12 -> "twelve"
13 -> "thirteen"
14 -> "fourteen"
15 -> "fifteen"
16 -> "sixteen"
17 -> "seventeen"
18 -> "eighteen"
19 -> "nineteen"
else -> throw IllegalArgumentException("Invalid input: $num")
}
} else {
val tensDigit = num / 10
val onesDigit = num % 10
val tensWord = when (tensDigit) {
2 -> "twenty"
3 -> "thirty"
4 -> "forty"
5 -> "fifty"
6 -> "sixty"
7 -> "seventy"
8 -> "eighty"
9 -> "ninety"
else -> throw IllegalArgumentException("Invalid input: $num")
}
val onesWord = convertSingleDigit(onesDigit)
return "$tensWord-$onesWord"
}
}
Step 3: Define a function to handle three-digit numbers
Now, let's define a function that can convert a three-digit number to its English word representation. We'll call this function convertThreeDigits.
private fun convertThreeDigits(num: Int): String {
val hundredsDigit = num / 100
val remainingDigits = num % 100
val hundredsWord = convertSingleDigit(hundredsDigit) + " hundred"
return if (remainingDigits == 0) {
hundredsWord
} else {
hundredsWord + " " + convertTwoDigits(remainingDigits)
}
}
Step 4: Define the main conversion function
Finally, let's define the main function that can convert any positive integer to its English word representation. We'll call this function convertToWord.
fun convertToWord(num: Int): String {
if (num == 0) {
return "zero"
}
if (num < 0) {
return "minus " + convertToWord(-num)
}
val billions = num / 1_000_000_000
val millions = (num / 1_000_000) % 1_000
val thousands = (num / 1_000) % 1_000
val remainingDigits = num % 1_000
val result = StringBuilder()
if (billions > 0) {
result.append(convertThreeDigits(billions)).append(" billion ")
}
if (millions > 0) {
result.append(convertThreeDigits(millions)).append(" million ")
}
if (thousands > 0) {
result.append(convertThreeDigits(thousands)).append(" thousand ")
}
if (remainingDigits > 0) {
result.append(convertThreeDigits(remainingDigits))
}
return result.toString().trim()
}
Step 5: Testing the conversion function
Let's test our convertToWord function with some sample inputs:
fun main() {
println(convertToWord(0)) // Output: zero
println(convertToWord(42)) // Output: forty-two
println(convertToWord(123)) // Output: one hundred twenty-three
println(convertToWord(1000)) // Output: one thousand
println(convertToWord(1_000_000)) // Output: one million
println(convertToWord(1_234_567_890)) // Output: one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
}
That's it! You now have a complete implementation to convert a number to its English word representation in Kotlin.