DecimalFormat will print fraction digit zeroes when formatting a BigDecimal with rounding enabled, even if # in pattern, eg:
orig BigDecimal: 50.000000004
format pattern : 00.00######
string result : 50.00000000 (instead of 50.00)
Example code to reproduce the problem:
=========================================
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.math.BigDecimal;
public class FormatTestICU
{
public static void main(String[] args)
{
String figure = "50.000000004";
Double dbl = new Double(figure);
BigDecimal dec = new BigDecimal(figure);
DecimalFormat f = (DecimalFormat) NumberFormat.getInstance();
f.applyPattern("00.00######");
System.out.println("Rounding none Double : " + f.format(dbl));
System.out.println("Rounding none BigDecimal : " + f.format(dec));
f.setRoundingIncrement(new BigDecimal("1").movePointLeft(f.getMaximumFractionDigits()));
f.setRoundingMode(BigDecimal.ROUND_DOWN);
System.out.println("Rounding down Double : " + f.format(dbl));
System.out.println("Rounding down BigDecimal : " + f.format(dec));
f.setRoundingIncrement(new BigDecimal("1").movePointLeft(f.getMaximumFractionDigits()));
f.setRoundingMode(BigDecimal.ROUND_HALF_UP);
System.out.println("Rounding half up Double : " + f.format(dbl));
System.out.println("Rounding half up BigDecimal: " + f.format(dec));
}
}
=======================================
Output will be:
Rounding none Double : 50,00
Rounding none BigDecimal : 50,00
Rounding down Double : 50,00
Rounding down BigDecimal : 50,00000000
Rounding half up Double : 50,00
Rounding half up BigDecimal: 50,00000000
kind regards
gottfried huber
Milestone 4.8RC deleted