3

I want to convert for example, 1000 to one thousand (currency). How can i do it in Jasper?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Chi
  • 395
  • 4
  • 11
  • 18
  • possible duplicate of [How can I convert an integer into its verbal representation?](http://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation) – Lorenzo Dec 20 '10 at 01:30
  • 1
    Not a duplicate. There are a number of technical details not covered by a simple link to a solution showing how to convert numbers to words in language X. Integration with iReport is a different question. – Dave Jarvis Jan 06 '11 at 19:34

2 Answers2

3
  1. See http://www.rgagnon.com/javadetails/java-0426.html
  2. Create a class based on the given implementation.
  3. Compile the class and put it in a directory where iReport can read the file.
  4. Update the CLASSPATH in iReport to point to the directory containing the class (be aware of directory relationships to package namespaces).
  5. Restart iReport.
  6. Change the text field expression to: EnglishNumberToWords.convert( $F{field_name} )

You will have to change field_name and the data type of the convert method according to your implementation details.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
  • did u mean change the field expression is "text field expression" in the text field properties. its appear error "EnglishNumberToWords cant be resolve" when i write "EnglishNumberToWords.convert( $F{dtl_docamt} )" – Chi Feb 16 '11 at 09:28
  • @Dave Jarvis: ive set the classpath, it still appear error. EnglishNumberToWords cannot be resolved. I also have change the field datatype to String – Chi Mar 01 '11 at 03:02
  • @Dave Jarvis: the class path is .class or .jar file? – Chi Mar 01 '11 at 04:19
  • 1
    @Chi: 1. Put the EnglishNumberToWords in a JAR file. 2. Add the JAR file to the CLASSPATH. 3. See this image: http://i.imgur.com/N6aPV.png – Dave Jarvis Mar 01 '11 at 04:32
1

An alternative to Dave's response:

1) If your RDBMS supports it (like HSQLDB, for example) you can create a user-defined, user-invoked function that takes the data model representation for a field and converts it to a presentation-layer representation. For example, a database stores timestamps internally as Modified Julian Day numbers (doubles). A Java function can be written and stored with the database (SQL/JRT) to convert from a UTC double to a localized time/date string.

2) Write an SQL Query to produce a table containing the data you want in the report. The difference is that you use your user-invoked SQL/JRT function on the source column to convert it to the presentation-layer representation in the Result Table.

3) Use your SQL Query (once you have it working) as the basis for a CREATE VIEW (DDL) statement.

4) Build your report using the newly defined View as the iReport datasource.

Advantages: No customization of iReports needed. The View you create can serve as the basis for any reporting tool, not only iReports.

Disadvantages: This creates a dependency between your database and a JRE and (most likely) your RDBMS. In order to access your user-invoked function, you'll need to store the function in the database and it will need to be able to access a JRE in order to create the View. There is a SQL/JRT standard and so it is possible that your migration target RDBMS might be able to support it, but certainly this is not ever guaranteed.

scottb
  • 9,908
  • 3
  • 40
  • 56