numberRenderer format?

Dear Team

            <column property="amount" editable="true" filterable="true">
               <numberRenderer format="#,##0.00"/>
            </column>

What format is expected. The above is not working (just displaying the pattern) and I don’t find any documentation about numberRenderer.

The property amount is a double.

Jmix version: 2.1.1
Jmix Studio plugin version: 2.1.2-233
IntelliJ version: IntelliJ IDEA 2023.3.2 (Ultimate Edition)

Best regards
Felix

Hello!

The format attribute takes the format according to String.format(). For instance:

  • with string formatting "Value: %s";
  • for Double with two numbers of decimal digits: "%.2f", etc.

If you need more complicated formatting, you can add renderer programmatically:

@Autowired
private CurrentAuthentication currentAuthentication;

@Supply(to = "dataGrid.columnWithDouble", subject = "renderer")
private Renderer<OrderLine> dataGridColumnWithDoubleRenderer() {
    DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance(currentAuthentication.getLocale());
    numberFormat.applyPattern("#,##0.00");
    return new NumberRenderer<>(MyEntity::getValue, numberFormat);
}

For more convenience I created an issue to support decimal formatting from renderer in XML: Support formatting via NumberFormat in numberRenderer · Issue #2732 · jmix-framework/jmix · GitHub

Hi Roman

Thank you for your reply.

To use "%.2f" was my first try; is there any reason, why the

numberDecimalSeparator=.

is not used ? The numbers are formatted like 200,00 in the listview. If i edit the field it is correctly formatted :wink:

grafik

grafik

The same behaviour with the programmatically renderer.

Kind regards

The numberDecimalSeparator is used in Jmix components via property Datatype, when the value should be formatted for presentation.

In case of %.2f we cannot configure which char will be used for group or decimal separation.

The example above just does not specify separator chars, but we can do the following:

@Autowired
private CurrentAuthentication currentAuthentication;
@Autowired
private FormatStringsRegistry formatStringsRegistry;

@Supply(to = "orderLinesDataGrid.value", subject = "renderer")
private Renderer<OrderLine> tasksDataGridTestRenderer() {
    DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance(currentAuthentication.getLocale());
    FormatStrings formatStrings = formatStringsRegistry.getFormatStrings(currentAuthentication.getLocale());
    
    numberFormat.setDecimalFormatSymbols(formatStrings.getFormatSymbols());
    numberFormat.applyPattern("#,##0.00");

    return new NumberRenderer<>(OrderLine::getValue, numberFormat);
}

We retrieve information about separator chars from FormatStringsRegistry and then from FormatStrings.