BigDecimal conversion back to presentation weird

Hi all,
I’m experiencing a strange issue.
On the presentation layer I’m using a simple text field, that via a BeanFieldGroup (non-buffered) is bound a BigDecimal in the model.
I try to calculate the total amount for an invoice. Just a base amount, multiplied by VAT%.
While debugging, I calculate things commited to the model and the update the text fields, but the strange thing is that the representation of the calculated BigDecimal into String is always having the decimal separator 2 places to the left. I get 2.415 instead of 241.5
Any ideas on how to solve this? I guess it’s about setting another format in the converter, but I expected it would be ok with the default converter.
Thanks in advance for any light,
Fran

That’s quite an issue. Out of curiousity I tried out some code and could not duplicate what you’re seeing. The only way I can get that something like that to happen is like this:

tf.addValueChangeListener(new Property.ValueChangeListener()
{
private static final long serialVersionUID = -6549081726526133772L;
public void valueChange(Property.ValueChangeEvent event)
{
String value = (String) event.getProperty().getValue();
System.out.println(“>>>>value: “+value);
BigDecimal bd = new BigDecimal(value);
BigDecimal ten = new BigDecimal(“10”);
bd = bd.divide(ten,3, BigDecimal.ROUND_HALF_UP);
System.out.println(”>>>>result: “+bd);
// method to set the text field value:
setTfValue(”” + bd);
}
});

Here’s what I get in the system log (and get 0.0000 in the text field):

value: 234.56789
result: 23.457
value: 23.457
result: 2.346
value: 2.346
result: 0.235
value: 0.235
result: 0.024
value: 0.024
result: 0.002
value: 0.002
result: 0.000
value: 0.000
result: 0.000

So it looks like there’s a ‘feedback loop’ where the TextField keeps feeding the value in, and the processing keeps happening. This is a contrived example where processing logic is taking place inside the event handler, but maybe something along these lines could be happening?

It does occur to me that changing a TextField dynamically is a little risky. Might it not be more appropriate to try displaying the computed value in a different place like a Label? At the very least, try temporarily setting the result in a label, and take away the code that’s putting it in the TextField and see if the correct result comes back.

Hi @codeanimal,
Thanks for the reply and the time spent.
I finally managed to fix it, but to be honest, I expected it to be more “out of the box”. Probably it has something to do with the Spanish locale, but in the end I provided my own (StringToCurrencyConverter) and modified the default converter factory to have this in place. Main method in the converter is:

@Override public String convertToPresentation(BigDecimal value, Class<? extends String> targetType, Locale locale) throws ConversionException { DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(LocaleContextHolder.getLocale()); otherSymbols.setDecimalSeparator('.'); DecimalFormat df = new DecimalFormat("#.##", otherSymbols); return df.format(value); } Hope it helps someone else
Cheers
Fran

Congrats!