Created
September 4, 2020 01:32
-
-
Save ajsaraujo/9ab91b71d14b831fcb683c2d53ef9b4a to your computer and use it in GitHub Desktop.
Máscara de Dinheiro em Java
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
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