7

Possible Duplicate:
formatting a string to a currency format in jasper report

I'm creating an invoice document using JasperReports that needs to be localized and support multiple currencies.

So for example when the report is in French, a currency value should be displayed as 1,00 € or 1,00 $ and when in US English it should be € 0.02 or $ 1.00.

Crucially, the invoice currency is often different from the locale's currency, and in some cases there may be several currencies used in the same document.

I've tried using the included formating tool:<textField pattern="¤ #,##0.00">, however this doesn't change the format according to the locale and I can't find a way of changing the currency (It only replaces the ¤ with the report's locale currency symbol, so wrong on both counts).

I've taken a look here: How do I format a Currency for a Locale in Java but haven't figured out a way of using this in my reports.

Thanks!



Well I finally figured it out, but since the mods locked the question, supposedly it's a duplicate — WHICH IT IS NOT... anyway, here is one answer:

<variable name="CURRENCY" class="java.util.Currency">
     <variableExpression><![CDATA[Currency.getInstance($F{currencyCode})]]></variableExpression>
</variable>

<variable name="CURRENCY_FORMAT" class="java.text.NumberFormat">
    <variableExpression><![CDATA[
NumberFormat.getCurrencyInstance($P{REPORT_LOCALE});
((NumberFormat)value).setCurrency($V{CURRENCY})
]]></variableExpression>
</variable>

<textField>
     <reportElement x="179" y="58" width="89" height="15"/>
     <textFieldExpression><![CDATA[$V{CURRENCY_FORMAT}.format($F{revenueCents}/100)]]></textFieldExpression>
</textField>
Community
  • 1
  • 1
ianaré
  • 3,230
  • 26
  • 26
  • 4
    This is not a duplicate!! The other question is about specifying the number format manually, while ianare wants to use the locale's default number format. Please, @Alex K et. al., read the full question again! – Bastian Voigt Jun 12 '14 at 07:29
  • 1
    Very good question. Not a duplicate. Someone who has the authority, please reopen. – HopeKing May 18 '18 at 03:56

1 Answers1

0

There are two parameters in ireport, REPORT_LOCALE and XML_LOCALE, can help

  • Yes I am already setting these parameters correctly according to the user language. My problem is that the formating of currency values doesn't take this into account, AND that I need to explicitly set the currency code in the report, and not use the default for the locale. – ianaré Nov 12 '12 at 17:43
  • Ack @ianare. Setting REPORT_LOCALE does not help – Bastian Voigt Jun 12 '14 at 07:27