1
votes

I'm using dynamic view panel in XPages. The problem that formatting the number does not follow the format defined in view column. I searched and found that the way to format the number is through a customizer bean. I've Got some code examples of this bean, though none of them had an example with formatting a column containing a number. Does anyone have an example of this treatment formatting?

Thanks a lot!

1

1 Answers

0
votes

Here's a code snippet from a getValueAsString() method from an ExtendedViewColumnConverter class that handles number formatting based on the user locale.

The code assumes that the variable currentLocale contains the user locale. currentLocale is an instance of java.util.Locale.Locale and I assume that you have logik that handles this e.g. as part of a user bean. If not, then one way of getting the current locale is to do this:

Locale currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

You should of course have an already working customizer bean (that extends DominoViewCustomizer).

public static class ExtendedViewColumnConverter extends ViewColumnConverter {

    ...

    @Override
    public String getValueAsString(final FacesContext context, final UIComponent component, final Object value) {

        if (value instanceof Number) {
            NumberFormat nf = NumberFormat.getInstance(currentLocale);
            DecimalFormat df = (DecimalFormat) nf;
            df.setRoundingMode(RoundingMode.HALF_UP);
            switch (this.colDef.getNumberFmt()) {
            case ViewColumn.FMT_GENERAL: {
                // no further formatting needed
                break;
            }
            case ViewColumn.FMT_FIXED: {
                df.setMinimumFractionDigits(this.colDef.getNumberDigits());
                df.setMaximumFractionDigits(this.colDef.getNumberDigits());
                break;
            }
            default: {
                // no further formatting needed
                break;
            }
            }
            return df.format(value);
        }
    }

    ...

}