Skip to main content

How to convert a number to its word representation with currency symbols in Kotlin

How to convert a number to its word representation with currency symbols in Kotlin.

Here is a step-by-step tutorial on how to convert a number to its word representation with currency symbols in Kotlin.

Step 1: Create a function to convert number to word representation

First, let's create a function that converts a given number to its word representation. We will use a recursive approach to break down the number into smaller parts and convert them to words. Here's an example implementation:

fun numberToWords(number: Long): String {
val units = arrayOf("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
val teens = arrayOf("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
val tens = arrayOf("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")

return when {
number < 0 -> "minus " + numberToWords(-number)
number < 10 -> units[number.toInt()]
number < 20 -> teens[number.toInt() - 10]
number < 100 -> tens[number.toInt() / 10] + " " + numberToWords(number % 10)
number < 1000 -> units[number.toInt() / 100] + " hundred " + numberToWords(number % 100)
number < 1000000 -> numberToWords(number / 1000) + " thousand " + numberToWords(number % 1000)
number < 1000000000 -> numberToWords(number / 1000000) + " million " + numberToWords(number % 1000000)
else -> numberToWords(number / 1000000000) + " billion " + numberToWords(number % 1000000000)
}
}

In this function, we use arrays to store the word representations of units, teens, and tens. We then use a series of conditions to determine the appropriate word representation based on the magnitude of the number.

Step 2: Add currency symbol to the word representation

To add a currency symbol to the word representation, we can modify the numberToWords function to include an additional parameter for the currency symbol. Here's an updated version of the function:

fun numberToWordsWithCurrency(number: Long, currency: String): String {
val numberWords = numberToWords(number)
return "$numberWords $currency"
}

In this updated function, we simply concatenate the word representation with the given currency symbol using string interpolation.

Step 3: Test the conversion

Now that we have our numberToWordsWithCurrency function, let's test it with some example numbers and currency symbols:

fun main() {
val number1 = 12345L
val currency1 = "$"
val result1 = numberToWordsWithCurrency(number1, currency1)
println("Number: $number1")
println("Word representation with currency: $result1")

val number2 = 987654321L
val currency2 = "€"
val result2 = numberToWordsWithCurrency(number2, currency2)
println("Number: $number2")
println("Word representation with currency: $result2")
}

When you run the program, you should see the word representations of the numbers along with the currency symbols:

Number: 12345
Word representation with currency: twelve thousand three hundred forty five $

Number: 987654321
Word representation with currency: nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one €

That's it! You have successfully converted a number to its word representation with currency symbols in Kotlin. Feel free to customize the function and add more currencies as needed.