So, the reason I want to do this is because I want to translate digits into my native language.
Normally:
val num = 1234
println(num) // toString() is called to stringify num before printing.
This is how kotlin works. But if I want the number in my native language by default, one solution might be to override Number.toString()
to covert the digits of the orginal toString()
output to print the digits in the language I want to.
Now the question is how do I actually do this? I tried something like this:
val digits = "০১২৩৪৫৬৭৮৯".toList()
open class Number {
override fun toString() : String {
return this.toString().map { it ->
if (!it.isDigit()) return@map it
return@map digits[it.digitToInt()]
}.joinToString("")
}
}
fun main() {
println(digits)
val test = 1234567890.0123456789
println(test) // still prints in english
}
But the following function works which means the problem is toString()
is not being overridden properly.
fun Number.tr(): String {
return this.toString().map { it ->
if (!it.isDigit()) return@map it
return@map digits[it.digitToInt()]
}.joinToString("")
}
Now how do I get the result I want?