I had setted maximum fraction digits as 4, but why is the output of N.doublevalue() 1234.56789999999 not 1234.5679?
try{
NumberFormat NF = NumberFormat.getInstance();
NF.setMaximumFractionDigits(4);
NF.setMinimumFractionDigits(3);
Number N = NF.parse("1234.56789999999");
System.out.println(N.doubleValue());
System.out.println(NF.format(1234.567899999));
}
catch(ParseException e){e.printStackTrace();}
}
The N.doubleValue()
actually converts your Number
to double
. double is an basic type, it doesn't support any rounding, it is raw floating point value written in memory, it doesn't have the exact value you try to convert it from, so instead it is the best match from the values possible for the double
. That said, double
does not have all values that are just possible, because there is a limited memory for it, the double data type is a double-precision 64-bit IEEE 754 floating point., the precision (places after dot) is bigger for small double
values and lower for bigger ones. Very big double
numbers doesn't even have any precision after dot at all. The same is with float
, but float has far less precision than double
.