Consider this program:
import java.text.NumberFormat; import java.text.ParseException; public class Main { public static void main(String[] args) { String[] numbers = {"1.222", "0.456789F"}; NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumMain(2); for(String number : numbers) { try { System.out.println(numberFormat.parse(number)); }//from ww w . j a v a 2s . c o m catch(ParseException pe) { System.out.println("Failed parsing " + number); } } } }
This program prints which of the following?
A) 1.22/*ww w . j ava2s. co m*/ 0.45 B) 1.22 0.46 C) 1.222 0.456789 D) 1.222 Failed parsing 0.456789 E) Failed parsing 1.222 0.456789 F) Failed parsing 1.222 Failed parsing 0.456789
C)
The parse()
method reads the values and converts it to Number if it succeeds.
So, it does not use the maximum fraction digits set using setMaximumMain()
; however, if it were to use the format()
method, which is meant for printing numbers, it will use this maximum fraction digits limit set.