Format money value according to locale and currency [duplicate]

点点圈 提交于 2020-01-31 11:47:33

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/13346939/format-money-value-according-to-locale-and-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!