Thursday, September 30, 2010

BigDecimal and localization

Hi,
Ever wonder why my currency is not coming in correct format or NumberFormat not giving error when forming wrong currency.
Here is the trick.
Try using ParsePosition and pass that object to NumberFormat parse method.
Then compare index of parseposition object with length of number string to get total length where NumberFormat has parsed the number.

here is the example.
try {
NumberFormat f = NumberFormat.getNumberInstance(new Locale("en", "US"));
ParsePosition p = new ParsePosition(0);
boolean error = false; // Do some thing when it becomes true.
BigDecimal bd = new BigDecimal(f.parse("100,59", p).doubleValue());
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
if(p.getIndex() != priceStr.length()) {
error = true;
}
}
catch(Exception e ) {
}

Friday, February 12, 2010

Jboss XA Datasource Example

Hi,
Here is example of Jboss XA DataSource. It works fine in jboss-eap-4.3. When i initially created i struggled little bit with NOClassDef Error, but after that it worked fine.