Rounding numbers in the table

Hello,

I want to ask that is the easiest way to round numbers in the table. I set data to the table from container.

table.setContainerDataSource(getContainer());

Some of the item properties values are floats, for example “100.50”, but I want them to be shown in the table rounded, should be “101”. What’s the easiest way of doing that?

Thank you :wink:

Hi,

one way would be to override the method

protected String formatPropertyValue(Object rowId, Object colId, Property property)

and return the value as a String, formatted like you see fit. You can do the formatting e.g. only for one column, based on the colId parameter and call the super class’ method for the rest.

If you are using Vaadin 7 you could also define a custom converter for rounded doubles


table.setConverter(propertyId, new RoundingDoubleConverter());

The advantage would be that you could re-use the converter in textfields or other parts of your application later. If the only place you need to round your numbers is that table, Teppo’s solution is probably easier.

Read more about creating your own converters here: https://vaadin.com/wiki/-/wiki/Main/Creating%20your%20own%20converter%20for%20String%20-%20MyType%20conversion