例如,如果我输入 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() 会更好;但我对这个方法不熟悉。 提前致谢!
最佳答案
尝试:
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);
关于java - 如何格式化TextField的文本? JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382250/