gpt4 book ai didi

java - 如何格式化TextField的文本? JavaFX

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:11 25 4
gpt4 key购买 nike

例如,如果我输入 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com