I'm using Java6 and would like to format a decimal value using this format mask "#,##0.00". At the first sight all looks fine but... I found that my decimal values are rounded using RoundingMode.HALF_EVEN rounding mode. And this is not correct in my case :( . Is this possible to specify an other rounding mode (i need HALF_UP mode)?
I can also use an internal formatting valibale (f.e java.text.DecimalFormat). But the same question is here: how it is possible to change a rounding mode?
You can change the default rounding mode of the number formatter by extending the default JR format factory (net.sf.jasperreports.engine.util.DefaultFormatFactory) and sending an instance of the extended factory via the REPORT_FORMAT_FACTORY parameter.
The rounding mode of a java.text.DecimalFormat instance can be changed via the setRoundingMode() method.
Is there a way to implement this change for a jasper server instance...or does this need to be made on a report by report basis?
Also, if we extend DefaultFormatFactory, do we just override createNumberFormat? If so, can you please let know (or point me to) the default implementation for that method?
Thanks in advance,
Post Edited by jdevine at 12/21/2009 17:36
To use a different format factory in JS, you'll need to change the JS as well (there's no JR configuration for the default format factory - you can log a feature request for that in the trackers).
Yes, you'd only need to override the createNumberFormat methoid. You can get the JR source code from the downloads section (the jr-x.y.z-project packages) or from the SVN (see the Source link above).
I've obtained the source, but I've realized that setRoundingMode is only available in Java 1.6 and we are currently using 1.5. Do you have any other suggestions to solve this problem (I'm not sure if migrating to 1.6 is feasible at this time)?
Also, when you say sending an instance of the extended factory via the REPORT_FORMAT_FACTORY parameter, what is the recommended way to do this? I see examples of Java sending the parameter and I see that you can specify formatFactoryClass="com.test.jasper.MyFormatFactory" in the jrxml, but I am not sure of the best solution. Since I need it to work through WebServices and interactively through the browser, I am currently setting the formatFactoryClass in the jrxml. Can you please let me know if this is the recommended solution?
Thanks in advance, Jim
Post Edited by jdevine at 01/04/2010 20:43
Achieving this in JR relies on support in the JDK. So if you want to do it with Java 1.5, it will be very difficult (essentially you'd have to implement your own number formatter which rounds values the way you want).
There's no recommended solution for using a custom format factory class. If using the JRMXL attribute makes sense in your case, you should use it.
This recently came up as a customer support issue, so I did a little research on it.
I looked into this a little bit--there are a couple parts to what the customer wants.
First is getting this to work in JasperServer. If you wanted to set a server-wide default format factory by passing in the parameter to every report, you could add a new BuiltInParameterProvider to the array in applicationContext.xml:
<bean id="builtInParameterProviders" class="java.util.ArrayList">
The source code of the UserProfileBuiltInParameterProvider has some more explanation of how it works.
There may also be some mechanism in JR to configure the default format factory through jasperreports.properties.
Second, how would you actually implement the new format factory in Java 5 or 6?
In either case you would start by subclassing DefaultFormatFactory.
Java 6 is easier--you can take the formatter returned from the superclass createNumberFormat() and set the rounding mode.
Java 5 is a little harder but not impossible. You would get the formatter returned from the superclass createNumberFormat(), then create a wrapper that implements format() by converting the number into a BigDecimal and doing the correct rounding on it (you would have to look at the DecimalFormat to see how many decimal places it contains).
Here's a good link for understanding rounding modes:
Post Edited by bobtins at 09/20/2010 16:48