1

I am trying to write values stored as BigDecimals e.g. 6789456123 to an xls file but I require the xls file to display $6,789,456,123 (As a currency with 0dp. I can do it as a string with this formatting however this ins't ideal). I am using JExcelApi and I am unable to figure out how to do this. Any help would be appreciated.

Thanks

Rudder_NZ
  • 57
  • 1
  • 5
  • you need to create a custom numberFormat http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/NumberFormat.html – MihaiC Dec 06 '14 at 22:29

1 Answers1

1

Edit: You can convert a BigDecimal to double with the method .doubleValue(). Changed example to meet your needs.


Try to create a custom NumberFormat like this:

NumberFormat dollarCurrency = new NumberFormat(NumberFormat.CURRENCY_DOLLAR + " ###,###,###.00", NumberFormat.COMPLEX_FORMAT);
WritableCellFormat dollarFormat = new WritableCellFormat(dollarCurrency);
n = new Number(column, row, myBigDecimal.doubleValue(), dollarFormat);
s.addCell(n);

You can also read up on many other example here: jxl demo

MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • As far as I can see this doesn't work as I am dealing with BigDecimals. Number has no constructor for (int,int,BigDecimal,WriteableCellFormat) – Rudder_NZ Dec 06 '14 at 22:41
  • @Rudder_NZ the 3rd paramaters is a double. use .doubleValue of method of Bigdecimal. So in your case pass: myBigDecimal.doubleValue() as the 3rd param – MihaiC Dec 06 '14 at 22:41
  • That worked thanks. For some reason I thought I had already tried that and it hadn't worked. – Rudder_NZ Dec 06 '14 at 22:45