PDA

View Full Version : BigDecimal issue



Reghin
17 Aug 2009, 5:48 AM
Hello guys.

In my application I have a need for real big numbers for monetary calculations. There is a BigDecimal for GWT (http://code.google.com/p/gwt-math/) that is supposed to help out on this.

When I edit (change the value) a NumberField I get an exception saying:


[ERROR] Uncaught exception escaped

java.lang.ClassCastException: java.lang.Double cannot be cast to java.math.BigDecimal
at br.com.ierp.gxt.entity.BeanModel_br_com_ierp_gxt_entity_Carteira.set(transient source for br.com.ierp.gxt.entity.BeanModel_br_com_ierp_gxt_entity_Carteira:224)
at com.extjs.gxt.ui.client.store.Record.set(Record.java:287)
at com.extjs.gxt.ui.client.binding.FieldBinding.updateModel(FieldBinding.java:178)
at com.extjs.gxt.ui.client.binding.FieldBinding.onFieldChange(FieldBinding.java:201)
at com.extjs.gxt.ui.client.binding.FieldBinding$1.handleEvent(FieldBinding.java:53)
at com.extjs.gxt.ui.client.binding.FieldBinding$1.handleEvent(FieldBinding.java:1)
at com.extjs.gxt.ui.client.event.BaseObservable.callListener(BaseObservable.java:176)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:96)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:444)
at com.extjs.gxt.ui.client.widget.form.Field.fireChangeEvent(Field.java:1005)
at com.extjs.gxt.ui.client.widget.form.Field.onBlur(Field.java:1046)
at com.extjs.gxt.ui.client.widget.form.TextField.onBlur(TextField.java:405)
at com.extjs.gxt.ui.client.widget.form.Field.onComponentEvent(Field.java:600)
at com.extjs.gxt.ui.client.widget.Component.onBrowserEvent(Component.java:760)
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1320)
at com.google.gwt.user.client.DOM.dispatchEventAndCatch(DOM.java:1299)
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1262)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke(IDispatchProxy.java:155)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.SwtHostedModeBase.processEvents(SwtHostedModeBase.java:235)
at com.google.gwt.dev.HostedModeBase.pumpEventLoop(HostedModeBase.java:558)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:405)
at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)
One thing to be noticed is that I do NOT get the error when I type any value for the first time (insert), and I also do NOT get any error when I display and populate the grid and fields in the form.

This is the code to set the NumberField to work with the BigDecimal class:


fieldValorPrevistoReal = new NumberField();
fieldValorPrevistoReal.setPropertyEditorType(BigDecimal.class);
Or this one:


fieldValorPrevistoReal = new NumberField();
fieldValorPrevistoReal.setPropertyEditor(new NumberPropertyEditor(BigDecimal.class));Neither of them seem to solve the problem.

How do I get GXT to support BigDecimal??

BTW: using GXT 2.0.1 and GWT 1.7.

Reghin
17 Aug 2009, 10:34 AM
Doing this solved the problem:


package br.com.ierp.gxt.client.ui.custom;

import java.math.BigDecimal;

import com.extjs.gxt.ui.client.widget.form.NumberPropertyEditor;

public class CustomNumberPropertyEditor extends NumberPropertyEditor {

public CustomNumberPropertyEditor(Class<?> type) {
super(type);
}

@Override
public Number convertStringValue(String value) {
try {
if (type == BigDecimal.class) {
return new BigDecimal(value);
}
} catch (Exception e) {
e.printStackTrace();
}
return super.convertStringValue(value);
}
}


To use it we do this:


fieldValorPrevistoReal = new NumberField();
fieldValorPrevistoReal.setPropertyEditor(new CustomNumberPropertyEditor(BigDecimal.class));


Hope it helps somebody. :)

pepez
2 Sep 2009, 3:36 AM
Good advice, thanks! =D>

erikhedb
15 Sep 2010, 5:08 AM
Hello all,

It seams strange that so few people run into this problem. When working with financial data you should always use BigDecimal or BigInteger.

Are most developers using float or double for money etc?

Anyway - we solved the above issue today but had to override more methods since we:

1: Use a , for decimal separation instead of US standard .
2. Need to format output

The number format of methods of NumberField makes use of NumberFormat and an explicit convertion to double.

public String getStringValue(Number value) {
if (format != null) {
return format.format(value.doubleValue());
}
return value.toString();
}

This will corrupt the Big data.

Comments anyone? Why is there so little support for Big types when most systems usually have financial data.

Thanx

/Erik

sven
15 Sep 2010, 5:15 AM
GWT does not support BigDecimal. GWT2.1 will add BigDecimal support. NumberFormat (from GWT) accepts a double, that is why we pass a double in.

erikhedb
15 Sep 2010, 8:55 PM
God morning :)

Yes but how do most developer get around this? Just put the head in the sand? We are going with our own solution based on GWT-MATH and modifications of the Gxt classes.

Also - when can we expect support for GWT 2.2? Could not find anything about it in the plans/notes for Gxt.

Thanks /Erik

sven
16 Sep 2010, 1:15 AM
Yes but how do most developer get around this? NumberFormat does not accept anything else than a double as of now. We cannot do anythign about this as this is not a GXT class. Also this is just for displayingthe value.

More relavant should be the part in post #2. We are going to look into things like this when GWT 2.2 moves out of milestone phase.