Loading...
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.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

BigDecimal conversion back to presentation weird

Francisco Serrano
6 years ago Jan 25, 2016 5:32pm
Dennis Wightman
6 years ago Jan 27, 2016 4:35am

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.

 

Francisco Serrano
6 years ago Jan 27, 2016 7:52am
Dennis Wightman
6 years ago Jan 27, 2016 4:04pm

Congrats!