Skip to content

Instantly share code, notes, and snippets.

@ajsaraujo
Created September 4, 2020 01:32
Show Gist options
  • Save ajsaraujo/9ab91b71d14b831fcb683c2d53ef9b4a to your computer and use it in GitHub Desktop.
Save ajsaraujo/9ab91b71d14b831fcb683c2d53ef9b4a to your computer and use it in GitHub Desktop.
Máscara de Dinheiro em Java
textField.textProperty().addListener((observable, oldValue, newValue) -> {
String numbersOnly = newValue.replace(",", "");
if (numbersOnly.matches("\\d+")) {
int valueInCents = Integer.parseInt(numbersOnly);
StringBuilder builder = new StringBuilder();
if (valueInCents >= 100) {
int integerPart = valueInCents / 100;
builder.append(integerPart).append(",");
} else {
builder.append("0,");
}
int cents = valueInCents % 100;
if (cents < 10) {
builder.append("0");
}
builder.append(cents);
textField.setText(builder.toString());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment