例如,如果我输入32164578542324236.97325074,则此数字应显示在TextField中,如32,164,578,542,324,236.97325074。 我试图处理这个问题,但没有成功:
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));
}
});
使用inputField.setTextFormatter()可能会更好;但我不熟悉这种方法。 提前谢谢!
答案 0 :(得分:0)
尝试:
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);