Following is clearly mentioned in java docs about java.text.NumberFormat class :-
setParseIntegerOnly : only affects parsing, e.g. if true, "3456.78" -> 3456 (and leaves the parse position just after index 6) if false, "3456.78" -> 3456.78 (and leaves the parse position just after index 8) This is independent of formatting.
Here is an example :-
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class NFTest {
public static void main(String args[]) throws Exception {
NumberFormat numberFormat = NumberFormat.getInstance();
//statement which is not going well
numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);
numberFormat.setParseIntegerOnly(true);
String usersNumber = "1976.0826" ;
try{
Number num = numberFormat.parse(usersNumber) ;
System.out.println(num);
}
catch(ParseException pe){
System.out.println(pe);
}
}
}
o/p :-
1976
Now, here again the same example with a slight change :-
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class NFTest {
public static void main(String args[]) throws Exception {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(true);
//result is getting effected, if i change the
//position of following statement
numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);
String usersNumber = "1976.0826" ;
try{
Number num = numberFormat.parse(usersNumber) ;
System.out.println(num);
}
catch(ParseException pe){
System.out.println(pe);
}
}
}
o/p :-
1976.0826
Why the result is getting effected due to change in the position of statements???