Skip to content

Instantly share code, notes, and snippets.

@jukiginanjar
Created January 18, 2021 02:42
Show Gist options
  • Save jukiginanjar/a310f7704c9a1f0814cd301c5ad4d332 to your computer and use it in GitHub Desktop.
Save jukiginanjar/a310f7704c9a1f0814cd301c5ad4d332 to your computer and use it in GitHub Desktop.
Convert Number to Indonesian Words
private const val TRILIUN = 1000000000000000
private const val MILIAR = 1000000000000
private const val JUTA = 1000000000
fun Long.toWords(): String {
return when (this) {
0L -> ""
1L -> "Satu"
2L -> "Dua"
3L -> "Tiga"
4L -> "Empat"
5L -> "Lima"
6L -> "Enam"
7L -> "Tujuh"
8L -> "Delapan"
9L -> "Sembilan"
10L -> "Sepuluh"
11L -> "Sebelas"
else -> {
return when {
this < 20 -> {
val first = (this - 10).toWords()
"$first Belas"
}
this < 100 -> {
val first = (this / 10).toWords()
val second = (this % 10).toWords()
"$first Puluh $second".trim()
}
this < 1000 -> {
val first = (this / 100)
val second = (this % 100).toWords()
return if (first == 1L) "Seratus $second".trim()
else "${first.toWords()} Ratus $second".trim()
}
this < 1000000 -> {
val first = (this / 1000).toWords()
val second = (this % 1000).toWords()
"$first Ribu $second".trim()
}
this < JUTA -> {
val first = (this / 1000000).toWords()
val second = (this % 1000000).toWords()
"$first Juta $second".trim()
}
this < MILIAR -> {
val first = (this / JUTA).toWords()
val second = (this % JUTA).toWords()
"$first Miliar $second".trim()
}
this < TRILIUN -> {
val first = (this / MILIAR).toWords()
val second = (this % MILIAR).toWords()
"$first Triliun $second".trim()
}
else -> {
""
}
}
}
}
}
fun Number.toWords(): String {
return toLong().toWords()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment