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>
There are two parameters in ireport, REPORT_LOCALE and XML_LOCALE, can help