I've implemented a Kotlin + Rx version.
It's for brazilian's currency (e.g. 1,500.00 - 5,21 - 192,90) but you can easily adapt for other formats.
Hope someone else finds it helpful.
RxTextView
.textChangeEvents(fuel_price) // Observe text event changes
.filter { it.text().isNotEmpty() } // do not accept empty text when event first fires
.flatMap {
val onlyNumbers = Regex("\\d+").findAll(it.text()).fold(""){ acc:String,it:MatchResult -> acc.plus(it.value)}
Observable.just(onlyNumbers)
}
.distinctUntilChanged()
.map { it.trimStart('0') }
.map { when (it.length) {
1-> "00"+it
2-> "0"+it
else -> it }
}
.subscribe {
val digitList = it.reversed().mapIndexed { i, c ->
if ( i == 2 ) "${c},"
else if ( i < 2 ) c
else if ( (i-2)%3==0 ) "${c}." else c
}
val currency = digitList.reversed().fold(""){ acc,it -> acc.toString().plus(it) }
fuel_price.text = SpannableStringBuilder(currency)
fuel_price.setSelection(currency.length)
}