JavaFx TextField货币格式过滤器 [英] JavaFx TextField Currency format filter

查看:100
本文介绍了JavaFx TextField货币格式过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实从用户那里获得了价格,但我想过滤数字和看跌期权,每12位中的3位数字都一样,例如123,123,123.

I do get the price from the user but I want to filter the digits and puts, in each 3 digits like 123,123,123.

txtPrice.textProperty().addListener((observable, oldValue, newValue) -> {
   if (!newValue.matches("\\d*")){
       txtPrice.setText(newValue.replaceAll("[^\\d]",""));
   }
});

推荐答案

要按照您指定的格式设置数字,您可以尝试以下操作:

To format number as you specified, you may try this:

// Eg: format "123123123" as "123,123,123"
if (newValue.matches("\\d*")) {
    DecimalFormat formatter = new DecimalFormat("#,###");
    String newValueStr = formatter.format(Double.parseDouble(newValue));

    txtPrice.setText(newValueStr);
}

希望这项帮助,祝您好运!

Hope this help, good luck!

这篇关于JavaFx TextField货币格式过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆