在碧玉报告中将字符串格式化为货币格式 [英] formatting a string to a currency format in jasper report

查看:179
本文介绍了在碧玉报告中将字符串格式化为货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字值的字符串。

我想用数字以逗号分隔的方式来格式化数字,数字之前有$美元符号。例如,

12345应格式化为$ 12,345.00

我尝试了下面的代码没有美元符号:

  new java.text.DecimalFormat(#,## 0.00).format。(myString)



$ p
$ b

 新的java.text.DecimalFormat($#,## 0.00)。格式。(myString)

然而,两者都给出了错误。

实现这种格式的正确方法是什么?

这是jasper报告jrxml的一部分,其中I要避免在报告空,并因此插入下面的代码:

 < textField isBlankWhenNull =falseisStretchWithOverflow = 真 > 
< reportElement stretchType =RelativeToTallestObjectx =1350y =0width =150height =30/>
< textElement />
< / textField>

其中myString是查询结果,并在jrxml中声明为:

 < field name =myStringclass =java.lang.String/> 

之前myString被声明为BigDecimal,但是比较运算符?=不起作用。

如果货币值不可用,我想在报告上打印不可用,而不是默认的null。否则,我想要按上述方式正确格式化数字。



如何解决这个问题?



谢谢阅读。

解决方案

正确的表达式是:

  new java.text.DecimalFormat($#,## 0.00)。format(Double.valueOf($ P {strParam}))

java.lang.Integer java.lang.String 的工作示例 UTF-8\" >?;
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //www.jasperreports.sourceforge.net/jasperreports.xsdname =format_as_currentpageWidth =595pageHeight =842whenNoDataType =AllSectionsNoDetailcolumnWidth =555 leftMargin =20rightMargin =20topMargin =20bottomMargin =20>
< parameter name =intParamclass =java.lang.Integer>
< defaultValueExpression><![CDATA [12345678]]>< / defaultValueExpression>
< / parameter>
< parameter name =strParamclass =java.lang.String>
< defaultValueExpression><![CDATA [12345678.95]]>< / defaultValueExpression>
< / parameter>
< title>
< band height =79splitType =Stretch>
< textField>
< reportElement x =137y =18width =291height =20/>
< textElement />
< textFieldExpression><![CDATA [new java.text.DecimalFormat($#,## 0.00).format($ P {intParam})]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =137y =48width =291height =20/>
< textElement />
<![CDATA [new java.text.DecimalFormat($#,## 0.00)。format(Double.valueOf($ P {strParam}!= null&& $ P {strParam} .length()> 0?Double.valueOf($ P {strParam}):0))]]>< / textFieldExpression>
< / textField>
< / band>
< / title>
< / jasperReport>

结果将会是(在 iReport 中预览):


$ b



注意:
您还应该添加检查null。



您也可以使用 textField pattern 属性来设置数据的格式。

示例:
$ b

 <?xml version =1.0encoding =UTF-8? > 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //www.jasperreports.sourceforge.net/jasperreports.xsdname =format_as_currentpageWidth =595pageHeight =842whenNoDataType =AllSectionsNoDetailcolumnWidth =555 leftMargin =20rightMargin =20topMargin =20bottomMargin =20>
< parameter name =intParamclass =java.lang.Integer>
< defaultValueExpression><![CDATA [12345678]]>< / defaultValueExpression>
< / parameter>
< parameter name =strParamclass =java.lang.String>
< defaultValueExpression><![CDATA [12345678.95]]>< / defaultValueExpression>
< / parameter>
< title>
< band height =148splitType =Stretch>
< textField pattern =$#,## 0.00>
< reportElement x =218y =99width =100height =20/>
< textElement />
< textFieldExpression><![CDATA [$ P {intParam}]]>< / textFieldExpression>
< / textField>
< textField pattern =$#,## 0.00>
< reportElement x =218y =119width =100height =20/>
< textElement />
< textFieldExpression><![CDATA [$ P {strParam}!= null&& $ P {strParam} .length()> 0? Double.valueOf($ P {strParam}):0]]>< / textFieldExpression>
< / textField>
< / band>
< / title>
< / jasperReport>

结果是一样的。


I have a string with some numeric value.

I want to format it in a way where hundreds are comma separated and the number is having $ dollar sign before it.

e.g. 12345 should be formatted to $ 12,345.00

I tried the below code without dollar sign:

new java.text.DecimalFormat(#,##0.00).format.(myString)

and the below one with dollar sign:

new java.text.DecimalFormat($ #,##0.00).format.(myString)

However, both are giving error.

What is the right way to achieve this format ?

This is a part of jasper report jrxml where I want to avoid "null" on the report and thus inserting the below code:

<textField isBlankWhenNull="false" isStretchWithOverflow="true">            
  <reportElement stretchType="RelativeToTallestObject" x="1350" y="0" width="150" height="30"/>             
      <textElement/>             
   <textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{myString}!=null?new java.text.DecimalFormat(#,##0.00).format.($F{myString}):"Unavailable"]]></textFieldExpression>        
</textField>

Where myString results from a query and is declared in jrxml as:

 <field name="myString" class="java.lang.String"/>

Earlier myString was declared as BigDecimal, but then comparison operator ?= was not working.

If the currency value is not available, I want to print "unavailable" on the report instead of default "null". Else, I want the number to be properly formatted as described above.

How to resolve this issue?

Thanks for reading.

解决方案

The correct expression is:

new java.text.DecimalFormat("$ #,##0.00").format(Double.valueOf($P{strParam}))

The working sample for java.lang.Integer and java.lang.String:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="format_as_current" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <parameter name="intParam" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[12345678]]></defaultValueExpression>
    </parameter>
    <parameter name="strParam" class="java.lang.String">
        <defaultValueExpression><![CDATA["12345678.95"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="137" y="18" width="291" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.text.DecimalFormat("$ #,##0.00").format($P{intParam})]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="137" y="48" width="291" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.text.DecimalFormat("$ #,##0.00").format(Double.valueOf($P{strParam} != null && $P{strParam}.length() > 0 ? Double.valueOf($P{strParam}) : 0))]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

The result will be (preview in iReport):

Note: You should also add check for null.

You can also use pattern property of textField for formatting data.

The sample:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="format_as_current" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <parameter name="intParam" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[12345678]]></defaultValueExpression>
    </parameter>
    <parameter name="strParam" class="java.lang.String">
        <defaultValueExpression><![CDATA["12345678.95"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="148" splitType="Stretch">
            <textField pattern="$ #,##0.00">
                <reportElement x="218" y="99" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{intParam}]]></textFieldExpression>
            </textField>
            <textField pattern="$ #,##0.00">
                <reportElement x="218" y="119" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{strParam} != null && $P{strParam}.length() > 0 ? Double.valueOf($P{strParam}) : 0]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

The result will be the same.

这篇关于在碧玉报告中将字符串格式化为货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆