Created
July 19, 2022 14:26
-
-
Save javlonrahimov/6f6bdcf47a5629695fda456d0950c111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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