列生成器 生成CurrencyField<BigDecimal>控件如设置 显示4位小数?

DTO实体加了@NumberFormat 如下图
image

如果是表单中这样写,可以正常显示4位小数
image
效果如下
image

如果是后台列生成器生成的CurrencyField 就不起效果,如何解决?如下图
image
即使我在后台设置的setValue里面是4位小数,页面会强制把值变成2位小数来显示,如下图
image

输入控件的格式是通过其 DataType 决定的。所以你的问题可以这么解决:

  1. 定义一个自己的Decimal DataType:
@DatatypeDef(
        id = "myDecimal",
        javaClass = BigDecimal.class
)
@Ddl("decimal")
public class MyDecimalType implements Datatype<BigDecimal> {
    private static final DecimalFormat format = new DecimalFormat("0.0000");

    @Override
    public String format(@Nullable Object value) {
        if (value == null)
            return "";

        return format.format(value);
    }

    @Override
    public String format(@Nullable Object value, Locale locale) {
        return format(value);
    }

    @Nullable
    @Override
    public BigDecimal parse(@Nullable String value) throws ParseException {
        if (Strings.isNullOrEmpty(value))
            return null;

        return BigDecimal.valueOf(format.parse(value).doubleValue());
    }

    @Nullable
    @Override
    public BigDecimal parse(@Nullable String value, Locale locale) throws ParseException {
        return parse(value);
    }
}
  1. 在初始化Field的时候,设置DataType:
yourField.setDatatype(new MyDecimalType());

会四舍五入,有什么办法能不进行四舍五入?我现在发现我输入 4.55555会变成4.5556

设置format的rounding mode 就可以了,比如:

public class MyDecimalType implements Datatype<BigDecimal> {
    private static final DecimalFormat format = new DecimalFormat("0.0000");

    static{
        format.setRoundingMode(RoundingMode.FLOOR); // 向下round
    }