0

I got trouble with format number in JasperReports. For example, i have a double value, and i put the format pattern like #,##0.000

Now, with value of number 1.123, i got what i want, but if the value is only 1, or 1.2 it return 1.000 and 1.200, not 1.2 or 1. How can i trim all unnecessary zero in this case.

Here is piece of jrxml:

<textField pattern="#,##0.000;-#,##0.000" isBlankWhenNull="true">
    <reportElement x="2390" y="0" width="60" height="25"/>
    <textFieldExpression><![CDATA[$F{C9} == null ? 0 : $F{C9}]]></textFieldExpression>
</textField>
Alex K
  • 22,315
  • 19
  • 108
  • 236
CNNPX
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [formatting a string to a currency format in jasper report](http://stackoverflow.com/questions/10913495/formatting-a-string-to-a-currency-format-in-jasper-report) & [How to nicely format floating numbers to String without unnecessary decimal 0?](http://stackoverflow.com/q/703396/876298) – Alex K Dec 08 '16 at 08:19

1 Answers1

2

That pattern is a standard Java pattern used to format decimal numbers. You can find more info in the Javadoc for DecimalFormat. The JasperReports engine internally feeds that pattern to a java.text.DecimalFormat instance.

In your case a more generic pattern like #,###.### should do it. There is no need for the negative pattern as it is optional and inferred from the positive one.

Simple Java code

NumberFormat format = new DecimalFormat("#,###.###");
System.out.println(format.format(1.200));
System.out.println(format.format(1.210));
System.out.println(format.format(1.213));
System.out.println(format.format(1.2137));

gives us results:

1,2
1,21
1,213
1,214
Alex K
  • 22,315
  • 19
  • 108
  • 236
Narcis
  • 2,421
  • 4
  • 16
  • 23