Convert a double to 2 decimal places with comma separator

In my view i have the below code

IndexedContainer icLoaded = new IndexedContainer();
icLoaded.removeAllItems();
icLoaded.removeAllContainerFilters();

icLoaded.addContainerProperty(“Average Cost”, Double.class, null);
In the model i have the below code

public IndexedContainer DoFetchstockCodesTable(String passedSQL,
IndexedContainer passTable, String CustomsaleQuerry) {

try {

    Class.forName(dbsettings.dbDriver);

    final Connection conn = DriverManager.getConnection(
            new Home().GiveMeSessionDB(), dbsettings.dbUsername,
            dbsettings.dbPassword);
    final Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery(passedSQL.trim());

    while (rs.next()) {

        passTable.addItem(rs.getString("item_id"));

        passTable.getContainerProperty(rs.getString("item_id"),
                "Average Cost").setValue(rs.getDouble("average_cost"));

How can i convert the below

passTable.getContainerProperty(rs.getString(“item_id”),
“Average Cost”).setValue(rs.getDouble(“average_cost”));
to display a number with 2 decimal places separated by a comma separator like 1,000.12 using

NumberFormat numberFormat = new DecimalFormat(“#,###.00”);
When i use the below , nothing gets displayed .

passTable.getContainerProperty(rs.getString(“item_id”),
“Average Cost”).setValue(numberFormat.format(rs.getDouble(“average_cost”)));

Hi!

First of all, your property type is Double:

icLoaded.addContainerProperty("Average Cost", Double.class, null);

But the formatter sets String values. Strange that you don’t get an IllegalArgumentException.
Setting the type to String should solve this, but then the sorting of that column will be sorted String-wise, which is not desirable.

So, a better option would be to let the container contain Double for that property id, but add a Converter to the table so that only the visual representation changes:

        passTable.setConverter("Average cost", new Converter<String, Double>() {

            @Override
            public Double convertToModel(String value,
                    Class<? extends Double> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
                if (value == null) {
                    return null;
                }
                return Double.valueOf(value);
            }

            @Override
            public String convertToPresentation(Double value,
                    Class<? extends String> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
                if (value == null) {
                    return null;
                }
                return numberFormat.format(value);
            }

            @Override
            public Class<Double> getModelType() {
                return Double.class;
            }

            @Override
            public Class<String> getPresentationType() {
                return String.class;
            }
        });

And as a little side note, I assume you want the format “#,##0.00” instead of “#,###.00” in case of zero values

Best regards,
Johan

Thanks Johan for a great solution . I want it in this format #,###.00. Cheers.