8

I need to print some big values. A value looks like this

2564894621

now I want to format the float value to this:

2.564.894.621

I am using Jaspersoft Studio to develop my jasper report.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Philipp Nies
  • 945
  • 4
  • 20
  • 38

1 Answers1

27

The best way to format in jasper report is to use the pattern attribute on the textField tag. This will keep correct class (Number), when exporting to for example excel, excel can identify it as number and will also apply same pattern.

Properties >> TextField >> Pattern

Either you know the correct pattern or you use the IDE to help you generate it

IDE

jrxml result

<textField pattern="#,##0">
   <reportElement x="0" y="0" width="200" height="25" uuid="ee49d149-394b-4ac6-a0a2-6d207b0c8d89"/>
   <textElement>
      <font fontName="DejaVu Serif" size="14"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{myNumber}]]></textFieldExpression>
</textField>

And exporting with a Locale that uses . as grouping separator it will display

Result

if your result is with the grouping separator , this does not depend on the pattern but simply your locale see this: How to invert the comma and dot when number formatting

In JasperSoft Studio the locale used during preview can be set in

Window>>Preferences>>Report Execution: Locale

Note: expression like

<textFieldExpression><![CDATA[new DecimalFormat("#,##0").format($F{myNumber})]]></textFieldExpression>

can be used as well but its better to avoid since the export manager will treat this as text only

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • I tried out the first method, the pattern works fine for me. Thanks for your helpful answer :-) – Philipp Nies Feb 11 '16 at 07:30
  • It works fine but Can I achieve this with code only with java, without changing it in jasper report. I tried with setLocale and setNumberPattern but it doesn't work quite well. For ex `85.5` will show like `8550.0` if i set german local (in csv value is 85.5). Help is appreciated, – Parth Patel Jul 14 '20 at 21:01
  • @Parth have you tried to set the report local fron java? – Petter Friberg Jul 15 '20 at 19:03
  • @PetterFriberg Yes I tried it in java as such `dataSource.setLocale(Locale.UK);` and i use `params.put(JRParameter.REPORT_LOCALE, Locale.GERMAN);` and my jasper print is like this `JasperPrint jasperPrint = JasperFillManager.fillReport(compiledTemplateInStreams, params, dataSource);` – Parth Patel Jul 31 '20 at 11:40
  • @ParthPatel 5 years later, better late then ever :) – Petter Friberg Jul 31 '20 at 18:41