Skip to content

Instantly share code, notes, and snippets.

@fanjavaid
Created August 15, 2022 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanjavaid/d7da3451db77bcc2514dd652ebf78a25 to your computer and use it in GitHub Desktop.
Save fanjavaid/d7da3451db77bcc2514dd652ebf78a25 to your computer and use it in GitHub Desktop.
Formatting input to thousand separator Jetpack Compose
@Composable
fun ThousandFormatTextField() {
var text by remember {
mutableStateOf(TextFieldValue(""))
}
TextField(
value = text,
onValueChange = { newInput ->
val newValue = if (newInput.text.isNotBlank()) {
newInput.text
.clearThousandFormat()
.toLong()
.formatThousand()
} else newInput.text
text = newInput.copy(
text = newValue,
selection = TextRange(newValue.length)
)
},
placeholder = {
Text(text = "Using textfieldvalue")
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment