stringToBigDecimalConverter display correct scale using Grid

Hello everyone,

My goal is to convert a BigDecimal to a string with the correct locale settings. In my case a fraction is written as: 3,45 and for Americans it would be 3.45. The method I use is more or less displayed in the example below:

StringToBigDecimalConverter stringToBigDecimalConverter = new StringToBigDecimalConverter("");
ValueContext context = new ValueContext();    
exportGrid.addColumn(ea -> stringToBigDecimalConverter.convertToPresentation(ea.getNettoValue(), context))

All my financial BigDeicmals hasve been processed with a setScale(2, RoundingMode.Half_Up); This due to that money related numbers are always shown with two zeroes. Currently it displays decimals like 3.5 as 3,5 whilst I want 3,50

Does anyone know how to configure the converter or the valuecontext so that it always show two decimals?

Just tested this out with a normal textfield. It shows the same behaviour.

The StringToBigDecimalConverter’s method convertToPresentation() looks like this:

public String convertToPresentation(T value, ValueContext context) {
	return value == null ? null : this.getFormat((Locale)context.getLocale().orElse((Object)null)).format(value);
}
protected NumberFormat getFormat(Locale locale) {
	if (locale == null) {
		locale = Locale.getDefault();
	}

	return NumberFormat.getNumberInstance(locale);
}

Which means that this should already be happening, provided that the locale is set to the locale of the user. This seems not to be the case with your app though. I can recommend reading about [Localization with Vaadin]
(https://vaadin.com/docs/v13/flow/advanced/tutorial-i18n-localization.html) to achieve this.

If you have a custom way of getting the locale of the user and want to use the converter to use a specific Locale, it should be possible to call convertToPresentation with a ValueContext that has the desired locale set.

StringToBigDecimalConverter stringToBigDecimalConverter = new StringToBigDecimalConverter("");
Locale desiredLocale = getUserLocale(); // however you want to find the locale that you want to use
ValueContext context = new ValueContext(desiredLocale); // initializing ValueContext with locale will result in that locale being used for conversion
exportGrid.addColumn(ea -> stringToBigDecimalConverter.convertToPresentation(ea.getNettoValue(), context))

Should you want to use a special NumberFormat that does not match the format used by the given Locale (i.e. if your american users tell you that they don’t want 3.45 but instead 3,45, then you’ll have to make your own Converter<String, BigDecimal> implementation, where you can use any NumberFormat that you want instead of depending on the number format defined by the locale.

There is also the possibility to add a column using a NumberRenderer, where you can pass your desired NumberFormat / DecimalFormat that will be applied.

exportGrid.addColumn(new NumberRenderer<>(MyClass::getNettoValue, getDesiredNumberFormat()));

Hey Kasper,

Thank you for the replies, thanks to your help among others on Gitter I’ve succeeded on solving this by extending the StringToBigDecimal converter. I’m very happy you gave the hint about the column one I will check that out!

Thanks