我有两种方法:
第一种方法-当用户键入内容时,使我的文本字段实时为小数
第二种方法-用(')千位运算符分隔我的数字的另一种方法
我想知道我的2种方法是否可以进行某种合并,但是我被卡住了。
我尝试了很多正则表达式示例,但似乎都没有用。这是我的代码
//the first method
public static void make_textfield_decimal(JFXTextField txt) {
txt.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d{0,9}([,.]\\d{0,2})?")) {
txt.setText(oldValue);
}
}
});
}
//the second method
public static void separate_digits(JFXTextField textField) {
final char seperatorChar = '\'';
final Pattern p = Pattern.compile("[0-9" + seperatorChar + "]*");
textField.setTextFormatter(new TextFormatter<>(c -> {
if (!c.isContentChange()) {
return c; // no need for modification, if only the selection changes
}
String newText = c.getControlNewText();
if (newText.isEmpty()) {
return c;
}
if (!p.matcher(newText).matches()) {
return null; // invalid change
}
// invert everything before the range
int suffixCount = c.getControlText().length() - c.getRangeEnd();
int digits = suffixCount - suffixCount / 4;
StringBuilder sb = new StringBuilder();
// insert seperator just before caret, if necessary
if (digits % 3 == 0 && digits > 0 && suffixCount % 4 != 0) {
sb.append(seperatorChar);
}
// add the rest of the digits in reversed order
for (int i = c.getRangeStart() + c.getText().length() - 1; i >= 0; i--) {
char letter = newText.charAt(i);
if (Character.isDigit(letter)) {
sb.append(letter);
digits++;
if (digits % 3 == 0) {
sb.append(seperatorChar);
}
}
}
// remove seperator char, if added as last char
if (digits % 3 == 0) {
sb.deleteCharAt(sb.length() - 1);
}
sb.reverse();
int length = sb.length();
// replace with modified text
c.setRange(0, c.getRangeEnd());
c.setText(sb.toString());
c.setCaretPosition(length);
c.setAnchor(length);
return c;
}));
}