arjacsoh arjacsoh - 1 year ago 73
Java Question

NumberFormat setMaximumFractionDigits method

I am trying out the methods and the functionality of the class NumberFormat and I have reached to a strange result. I compile and run the following program:

public static void main(String[] args) {

Locale loc = Locale.US;
NumberFormat nf = NumberFormat.getInstance(loc);
System.out.println("Max: "+nf.getMaximumFractionDigits());
System.out.println("Min: "+nf.getMinimumFractionDigits());
try {
Number d = nf.parse("4527.9997539");
// nf.setMaximumFractionDigits(4);
} catch (ParseException e) {
// TODO Auto-generated catch block


The outout is:

Max: 3

Min: 0



That means that it does not take into account any fraction digit. If I uncomment the line:


The output is:

Max: 3

Min: 0



In other words it works OK. What is actually happening with method setMaximumFractionDigits and it does not bring the number containing 3 fraction digits in the first case?

Answer Source

I found the answer finally. The method setMaximumFractionDigits() has an effect only on the method format(). It has nothing to do with parse(). In my code fragment I use the method format() after setting manually the numberof fraction digits to 4, consequently it affects the result.