Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Grid only saving 3 decimal places when mapping column to BigDecimal or Doub
I think I found another undesirable issue with the Grid. When mapping a BigDecimal to a column, only 3 decimal places are being saved. Interestingly, it is rounding, so I am under the impression that this is designed this way for some reason.
I have attached a very simple project that demonstrates this. If you try running the application and editing the Value column, you will see that all data gets rounded at 3 decimal places. (by the way, to save a value to the Grid you need to use the UP/DOWN arrows ... don't ask)
How do I change the number of decimal places?
In the actual project, I managed to switch to double where decimal places was pracitaclly infinite, but then I get precision then got funky with some decimals and worse it can't handle NULL values. Changing to Double, however, put me back to where I started, only storing 3 decimal places.
Am I doing something wrong? Maybe I am missing something obvious?
It's the StringToBigDecimalConverter that is converting BigDecimal to String using rounding. You need to provide your own converter that has more decimal places reserved for presentation.
Thanks, Johannes. I'm sorry to ask, but do you have any insight as to how this can be done? I found examples of the converter online, and see how to fix it, but am unclear as to how to bind my new converter to the Grid.
Grid.getColumn(propertyId).setConverter() would be the method you need.
OK, now I feel stupid. I am obviously close, but I can't figure out what I'm doing wrong. I have implemented my converter, and know it is being called because I can see convertToPresentation is being called, but nothing I do seems to effect the number of decimal places it recognizes.
I have overriden all the functions in AbstractStringToNumberConverter, and still nothing. Outside of convertToPresentation, it seems the other functions are dead. Here's where I'm at now (Its weird, I know, but I've tried a lot), maybe someone can point me in the right direction?
public class StringToBigDecimalConverter extends AbstractStringToNumberConverter<BigDecimal> {
@Override
protected NumberFormat getFormat(Locale locale) {
NumberFormat numberFormat = super.getFormat(locale);
if (numberFormat instanceof DecimalFormat) {
((DecimalFormat) numberFormat).setParseBigDecimal(true);
}
return numberFormat;
}
@Override
public String convertToPresentation(BigDecimal value, Class<? extends String> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
System.out.println(" ... presentation ... " + value);
return value.setScale(4, RoundingMode.HALF_UP).toString();
}
@Override
protected Number convertToNumber(String value, Class<? extends Number> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
System.out.println(" ... number ... " + value);
return Util.toBigDecimal(value).setScale(4, RoundingMode.HALF_UP);
}
@Override
public BigDecimal convertToModel(String value, Class<? extends BigDecimal> aClass, Locale locale) throws ConversionException {
BigDecimal retval = Util.toBigDecimal(value).setScale(4, RoundingMode.HALF_UP);
System.out.println(" ... model ... " + value);
return retval;
}
@Override
public Class<BigDecimal> getModelType() {
return BigDecimal.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}
Just to be sure, have you added the converter also to the editor textbox?
To the textbox? ... No, I don't think so. I added it to the Grid's column like so:
myGrid.getColumn("nav").setConverter(new StringToBigDecimalConverter());
Is there another step to this?
I'm not 100% sure, but I think Grid doesn't assign the column's converter to the editor field created for the column. Here's an old (but still valid) article on how to assign your own converter as the default for a type. You could try if that fixes your issue. https://vaadin.com/wiki/-/wiki/Main/Changing+the+default+converters+for+an+application