本文介绍了你能做一个TextField< BigDecimal>接受两者,和。作为小数分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Wicket应用程序中,我有一个十进制数字文本字段:

In a Wicket app, I have a decimal number text field:

 TextField<BigDecimal> f =
     new TextField<BigDecimal>("f", new PropertyModel<BigDecimal>(model, "share"));

我希望它始终接受两者。 (点)和,(逗号)作为小数点分隔符(无论是否为浏览器的区域设置)。

I want it to always accept both . (dot) and , (comma) as decimal separator (regardless of browser's locale settings).

对于显示值的,使用会话的语言环境[在我们的例子中强制为fi( - >逗号)],但是在这里我对字段接受作为输入感兴趣。

For showing the value, session's locale is used [which in our case is forced to be "fi" (-> comma)], but here I'm interested in what the field accepts as input.

我的问题是,我是否必须将字段更改为 TextField< String> ,并手动转换为域对象的类型(BigDecimal)?或者有没有办法使用 TextField< BigDecimal> (允许使用Wicket的MinimumValidator或RangeValidator),并且仍然接受两个小数分隔符?

My question is, do I have to change the field to TextField<String>, and convert to domain object's type (BigDecimal) manually? Or is there some way to use TextField<BigDecimal> (which allows e.g. making use of Wicket's MinimumValidator or RangeValidator), and still have it accept both decimal separators?

推荐答案

感谢@ bert的评论以及 Wicket in Action 一书,我发现了一种有效的方法。在Application类中为BigDecimals指定一个自定义转换器:

Thanks to @bert's comment, and the Wicket in Action book, I found an approach that works. In the Application class specify a custom converter for BigDecimals:

@Override
protected IConverterLocator newConverterLocator() {
    ConverterLocator converterLocator = new ConverterLocator();
    converterLocator.set(BigDecimal.class, new CustomBigDecimalConverter());
    return converterLocator;
}

在自定义转换器中, convertToObject 需要被覆盖。 NB :这足以满足我们的需求;考虑你的要求并根据需要进行调整!

And in the custom converter, convertToObject needs to be overridden. NB: this is sufficient for our needs; think about your requirements and adapt as needed!

public class CustomBigDecimalConverter extends BigDecimalConverter {

    @Override
    public BigDecimal convertToObject(String value, Locale locale) {
        // NB: this isn't universal & your mileage problably varies!
        // (Specifically, this breaks if '.' is used as thousands separator)
        if ("fi".equals(locale.getLanguage())) {
            value = value.replace('.', ',');
        }
        return super.convertToObject(value, locale);
    }
}

编辑:Offtopic,但我也要记录这一点。我们需要我们的应用程序支持4位小数的比例,我们的自定义BigDecimal转换器也很好地解决了这个问题。

Edit: Offtopic, but I want to document this too. We needed our app to support a scale of 4 decimal places, and our custom BigDecimal converter nicely solves that problem too.

  @Override
    public String convertToString(Object value, Locale locale) {
        NumberFormat fmt = getNumberFormat(locale);
        fmt.setMaximumFractionDigits(4); // By default this is 3.
        return fmt.format(value);
    }

此自定义后,像2.0005这样的十进制数字将显示为2.0005而不是2。

After this customisation, a decimal number like 2.0005 will be shown as 2.0005 instead of 2.

这篇关于你能做一个TextField&lt; BigDecimal&gt;接受两者,和。作为小数分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:53