Skip to content

Instantly share code, notes, and snippets.

@javlonrahimov
Created July 19, 2022 14:26
Show Gist options
  • Save javlonrahimov/6f6bdcf47a5629695fda456d0950c111 to your computer and use it in GitHub Desktop.
Save javlonrahimov/6f6bdcf47a5629695fda456d0950c111 to your computer and use it in GitHub Desktop.
fun moneyFilter(text: AnnotatedString): TransformedText {
return TransformedText(
text = AnnotatedString(text.text.toLongOrNull()?.formatWithComma() ?: ""),
offsetMapping = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
return text.text.toLongOrNull()?.formatWithComma()?.length ?: 0
}
override fun transformedToOriginal(offset: Int): Int {
return text.length
}
}
)
}
fun Long.formatWithComma(): String =
NumberFormat.getNumberInstance(Locale.US).format(this ?: 0)
TextField(
value = amountState.value,
onValueChange = {
// change state
}
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
visualTransformation = {
moneyFilter(it)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment