This post will give you the inputs on how to get the non rounded(exact values) after decimal points for Double or Big Decimal types in Jasper Reports/(Java).
There are situations in reporting to display the exact values after decimal point for some purpose.
For example :
4.53455 should get printed as 4.53
0.234 should get printed as 0.23,
0.057 should get printed as 0.05 (but not 0.06 by rounding)
0.389 should get printed as 0.38 (but not 0.39 by rounding)
The above implementation in JAVA is pretty easy but in jasper it would be a tricky as shown in below examples.
Examples :
new BigDecimal(new Double(0.05592538471251264)).setScale(1, RoundingMode.DOWN)
new BigDecimal(new Double(1.9596988841721044)).setScale(1, RoundingMode.DOWN)
new BigDecimal(new Double(91.0)).setScale(1, RoundingMode.DOWN)
new BigDecimal(new Double(.0)).setScale(1, RoundingMode.DOWN)
Sample output :
Sample JRXML : ( Copy paste the below code in your source in professional studio)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.1.final using JasperReports Library version 6.0.0 -->
<!-- 2015-12-08T13:29:09 -->
<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="Blank_A4_1" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="927e91d9-da47-46ff-b6a9-744c47f3dff0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="298" splitType="Stretch">
<textField>
<reportElement mode="Opaque" x="153" y="72" width="180" height="40" backcolor="#BFF2CF" uuid="a39e7410-e811-4545-81b2-94d0239c43ee"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[String.format("%.1f",0.05592538471251264)]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="153" y="122" width="180" height="30" backcolor="#EFDAF7" uuid="69394848-d852-4f9f-b3b3-f644e06608ed"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new BigDecimal(new Double(0.05592538471251264)).setScale(1, RoundingMode.DOWN)]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="-1" y="122" width="154" height="30" backcolor="#EFDAF7" uuid="4b65ff63-e767-43a4-806e-0b8548175946"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["0.05592538471251264"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="150" y="160" width="183" height="30" backcolor="#EFDAF7" uuid="6595f244-af18-4c15-aa98-95494397055b"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new BigDecimal(new Double(1.9596988841721044)).setScale(1, RoundingMode.DOWN)]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="-1" y="160" width="151" height="30" backcolor="#EFDAF7" uuid="5088a6a2-d02c-4461-81e5-afeb3d0e667f"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["1.9596988841721044"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="353" y="122" width="180" height="30" backcolor="#EFDAF7" uuid="7b137a50-3266-48e1-b6f6-5d96c706e7aa"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["("+new BigDecimal(new Double(0.05592538471251264)).setScale(1, RoundingMode.DOWN) +"%"+")"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="352" y="161" width="183" height="30" backcolor="#EFDAF7" uuid="eb1c793d-be6b-4140-ab17-403e509b70a2"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["("+new BigDecimal(new Double(1.9596988841721044)).setScale(1, RoundingMode.DOWN)+"%"+")"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="-1" y="203" width="154" height="30" backcolor="#EFDAF7" uuid="2be78c2f-121a-420f-840a-4053ca87699c"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["91.0"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="355" y="204" width="183" height="30" backcolor="#EFDAF7" uuid="365e3fae-e19e-4f49-aa7a-020438455903"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["("+new BigDecimal(new Double(91.0)).setScale(1, RoundingMode.DOWN)+"%"+")"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="153" y="203" width="183" height="30" backcolor="#EFDAF7" uuid="3ffc22a8-ce74-4780-a6ab-af43b9e1b10d"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new BigDecimal(new Double(91.0)).setScale(1, RoundingMode.DOWN)]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="152" y="248" width="183" height="30" backcolor="#EFDAF7" uuid="c7450588-2e81-44a8-bff9-c96a7fa645dd"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new BigDecimal(new Double(.0)).setScale(1, RoundingMode.DOWN)]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="354" y="249" width="183" height="30" backcolor="#EFDAF7" uuid="fb7c9922-230e-475c-afe1-1aa0b675097a"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["("+new BigDecimal(new Double(.0)).setScale(1, RoundingMode.DOWN)+"%"+")"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="-1" y="248" width="153" height="30" backcolor="#EFDAF7" uuid="6d9ccc44-f198-499b-9385-c2d25bd2a46b"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[".0"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="1" y="72" width="151" height="40" backcolor="#BFF2CF" uuid="a5f36874-8b7c-45cd-bd3b-f8cbe33c2418"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["0.05592538471251264"+"\n"+"Using String format"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="353" y="72" width="180" height="40" backcolor="#BFF2CF" uuid="2f2688c8-b600-4acc-a069-7a22fe263ae8"/>
<box rightPadding="10">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["("+String.format("%.1f",0.05592538471251264) +"%"+")"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="0" y="21" width="555" height="30" backcolor="#E3E1E3" uuid="c103067c-da2e-4091-b18c-357aebf5e113"/>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Display RoundingMode.DOWN values for Decimal Types in Jasper Reports"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
References :
1) http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
2) http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html
No comments:
Post a Comment