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

标签 java text javafx formatting textfield

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

相关文章:

java - 如何重置EventStream中最后的累加值?

java - 无法使用 Vaadin 导航器进行重定向

c# - 从 PDF 中仅提取粗体文本的最佳方法

java - 文本文件错误扫描仪

jquery .text() 未确定的字符串文字

JavaFX - 使用其他类作为节点

java - 在一维中调整 JFrame 的大小

java - 从动态创建的 editText 获取值输入

java - SwingWorker 中的线程安全 - 以线程安全的方式更新 JTable

javafx - ImageView 上的边框半径和阴影