2

For example, if I enter 32164578542324236.97325074, this number should be displayed in a TextField like 32,164,578,542,324,236.97325074. I tried to handle this, but unsuccessfully:

    inputField.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            BigDecimal bd = new BigDecimal(newValue);
            NumberFormat formatter = NumberFormat.getNumberInstance();             
            inputField.setText(formatter.format(newValue));
        }
    });

Maybe it would be better to use inputField.setTextFormatter(); but I'm not familiar with this method. Thanks in advance!

void
  • 731
  • 2
  • 11
  • 26
  • Please use "Search" of this site before asking. See [this](http://stackoverflow.com/a/31043122) – Uluk Biy Jul 13 '15 at 11:47
  • I saw that page, but it's hard to understand how this method works by that example. – void Jul 13 '15 at 12:10
  • Basically you need to configure the pattern of DecimalFormat in that example. Please spend some time to understand the DecimalFormat pattern and other configs (like thousand separator) by reading its javadoc, if that is also hard, search the net for more detailed tutorials with sample codes. – Uluk Biy Jul 13 '15 at 12:18

1 Answers1

0

Try :

BigDecimal bd = new BigDecimal(newValue);
DecimalFormat formatter  = new DecimalFormat(",###");// seperate them by ',' while digits after '.' are excluded
String newestValue = formatter.format(bd)+newValue.substring(newValue.indexOf("."));// concatenate with the digits after '.'
inputField.setText(newestValue);
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19