I\'m using Java with Jasper Reports and would like to format a decimal value using this format mask \"#,##0.00\"
. At the first sight all looks fine, but I found
Here is another solution.
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 JRParameter.REPORT_FORMAT_FACTORY parameter.
The rounding mode of a java.text.DecimalFormat instance can be changed via the setRoundingMode() method.
https://community.jaspersoft.com/questions/528989/decimal-formatting-rounding-mode-problem
I've created inner class:
getParameters().put(REPORT_FORMAT_FACTORY, new DefaultFormatFactory() {
@Override
public NumberFormat createNumberFormat(String pattern, Locale locale) {
NumberFormat format = null;
if (pattern != null && pattern.trim().length() > 0) {
if (STANDARD_NUMBER_FORMAT_DURATION.equals(pattern)) {
format = new DurationNumberFormat();
} else {
format = locale == null ?
NumberFormat.getNumberInstance() :
NumberFormat.getNumberInstance(locale);
if (format instanceof DecimalFormat) {
format.setRoundingMode(HALF_UP);
((DecimalFormat) format).applyPattern(pattern);
}
}
}
return format;
}
});`