DecimalFormat 点和逗号作为 decimalSeparator
DecimalFormat point and comma as decimalSeparator
我曾经写过这段代码来格式化 JFormattedTextField
的输入
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
NumberFormatter nf = new NumberFormatter(decimalFormat);
nf.setValueClass(Double.class);
nf.setAllowsInvalid(true);
nf.setOverwriteMode(false);
tfPreisIntern = new JFormattedTextField(nf);
一段时间后,我意识到如果用户输入 4,50
而不是 4.50
然后值设置为 4.00
而不是 4.50
会出现问题或 4,50
。我认为这是我 DecimalFormat
的问题。现在我的问题是,是否可以使用逗号和点作为小数点分隔符?解决方法也可以
我将值从 JFormattedTextField
转换为 BigDecimal
的方法:
public static BigDecimal getBigDecimal(Object value) {
BigDecimal ret = null;
if (value != null) {
if (value instanceof BigDecimal) {
ret = (BigDecimal) value;
} else if (value instanceof String) {
ret = new BigDecimal((String) value);
} else if (value instanceof BigInteger) {
ret = new BigDecimal((BigInteger) value);
} else if (value instanceof Number) {
ret = new BigDecimal(((Number) value).doubleValue());
} else {
throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
+ " into a BigDecimal.");
}
}
return ret;
}
我在这里看到的逗号问题是构造函数 BigDecimal(string)
只允许小数点而不是逗号
编辑:
现在尝试像这样 MaskFormatter
:
MaskFormatter mask = new MaskFormatter("*#.##");
mask.setPlaceholderCharacter(' ');
mask.setCommitsOnValidEdit(false);
但我无法输入 2.50
这样的值。这只有在我使用像 #.##
这样的掩码时才有效。但是我需要在点
之前有 1 位和 2 位数字的可能性
当您检查 Oracle tutorial 时,您会发现:
- 对于格式化,可以改变格式化符号,但是
- 对于解析没有这样的东西。
这很有道理:您只是在问 "I want the user to be able all kind of inconsistent data; and code shall magically turn that into what the user meant"。
改善用户体验的一种方法:代替使用格式化的文本字段;您可以简单地让用户输入 strings。然后你编写 验证器 允许 "magic" 发生。
换句话说:如果您希望“4,59”和“4.59”的结果相同,那么您需要为此编写代码。这可能就像使用 indexOf()
来计算“.”一样简单。或“,”出现在传入字符串中 (ONCE);然后尝试使用 "formatted" 对每个使用不同模式的案例进行解析。如果您决定走那条通往深渊的陡峭路线,请开始阅读 InputVerifiers
here 例如。
但当然:这可能会变得很复杂;因为如前所述;您基本上允许用户输入 任何 数据;并期望猜他的意思。
我个人的二分钱:在该文本字段周围放置很多 warnings/examples;并允许完全 one 基于语言环境的输入。当他违反时,打用户的手指。
我曾经写过这段代码来格式化 JFormattedTextField
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
NumberFormatter nf = new NumberFormatter(decimalFormat);
nf.setValueClass(Double.class);
nf.setAllowsInvalid(true);
nf.setOverwriteMode(false);
tfPreisIntern = new JFormattedTextField(nf);
一段时间后,我意识到如果用户输入 4,50
而不是 4.50
然后值设置为 4.00
而不是 4.50
会出现问题或 4,50
。我认为这是我 DecimalFormat
的问题。现在我的问题是,是否可以使用逗号和点作为小数点分隔符?解决方法也可以
我将值从 JFormattedTextField
转换为 BigDecimal
的方法:
public static BigDecimal getBigDecimal(Object value) {
BigDecimal ret = null;
if (value != null) {
if (value instanceof BigDecimal) {
ret = (BigDecimal) value;
} else if (value instanceof String) {
ret = new BigDecimal((String) value);
} else if (value instanceof BigInteger) {
ret = new BigDecimal((BigInteger) value);
} else if (value instanceof Number) {
ret = new BigDecimal(((Number) value).doubleValue());
} else {
throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
+ " into a BigDecimal.");
}
}
return ret;
}
我在这里看到的逗号问题是构造函数 BigDecimal(string)
只允许小数点而不是逗号
编辑:
现在尝试像这样 MaskFormatter
:
MaskFormatter mask = new MaskFormatter("*#.##");
mask.setPlaceholderCharacter(' ');
mask.setCommitsOnValidEdit(false);
但我无法输入 2.50
这样的值。这只有在我使用像 #.##
这样的掩码时才有效。但是我需要在点
当您检查 Oracle tutorial 时,您会发现:
- 对于格式化,可以改变格式化符号,但是
- 对于解析没有这样的东西。
这很有道理:您只是在问 "I want the user to be able all kind of inconsistent data; and code shall magically turn that into what the user meant"。
改善用户体验的一种方法:代替使用格式化的文本字段;您可以简单地让用户输入 strings。然后你编写 验证器 允许 "magic" 发生。
换句话说:如果您希望“4,59”和“4.59”的结果相同,那么您需要为此编写代码。这可能就像使用 indexOf()
来计算“.”一样简单。或“,”出现在传入字符串中 (ONCE);然后尝试使用 "formatted" 对每个使用不同模式的案例进行解析。如果您决定走那条通往深渊的陡峭路线,请开始阅读 InputVerifiers
here 例如。
但当然:这可能会变得很复杂;因为如前所述;您基本上允许用户输入 任何 数据;并期望猜他的意思。
我个人的二分钱:在该文本字段周围放置很多 warnings/examples;并允许完全 one 基于语言环境的输入。当他违反时,打用户的手指。