Formatting currency in Jasper Reports using pattern

房东的猫 提交于 2019-12-01 19:26:41

问题


I have a query which returns amount from a table:

select bus_price from mySchema.BusTable;

This will return amounts like:

526547
123456
456789.25
12478.35

I am using above amounts in jasper report.

However, I want the output in the report to be displayed as:

$526,547.00
$123,456.00
$456,789.25
$12,478.35

JRXML code snippet is:

<textField isStretchWithOverflow="true">        
     <reportElement stretchType="RelativeToTallestObject" x="700" y="0" width="100" height="30"/>                               
     <textElement/>             
       <textFieldExpression class="java.math.BigDecimal">
         <![CDATA[$F{BusPrices}]]>
      </textFieldExpression>         
</textField>

I know I have to use patterns. However, I am not able to make it work.

Using

<textField isStretchWithOverflow="true" pattern='$###,##0.00'>        

is not working.

What am I missing ??

Thanks for reading!


回答1:


You were close

<textField pattern="¤ #,##0.00">

should work.

You need the "¤" character to make it a "currency" format.

If you download iReports with a version number that matches your server you find out what your options are.




回答2:


if you want to display currency symbol in $ and currency code is USD use the following pattern in jasper report. pattern = $ #,##0.00




回答3:


It might depend on your version of Jasper.

On 3.7.6 I found that specifying the textFieldExpression class as java.util.BigDecimal meant that the pattern isn't applied. Maybe that version of Jasper doesn't realise it is a number.

If you choose instead specify the textFieldExpression class as being "java.lang.Number", it will use the pattern when you pass a BigDecimal in.



来源:https://stackoverflow.com/questions/8758680/formatting-currency-in-jasper-reports-using-pattern

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