Have you read the documentation for Double#toString()?
Look at our FAQ (no 20). It tells you about nearest value to distinguish a double from its adjacent value. It might be better to ask "does Double.toString() ever return exact value?"
according to IEEE 754,
the really value of 2.3 stored in memory should be 2.29999999999999982236431605997495353221893310546875.
why are the first 2 lines 2.3?
it seems that java stores floating-point types in IEEE 754 format, but does some kind of rounding off when using them.
does anyone know why java deals with double this way?
is there any specification about it?
How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double.
In the case of 2.3, no other possible double is closer to 2.3 than the one you call d1 is, so "2.3" is sufficient to uniquely identify that value - even if it doesn't look exactly the same.
2) Why does String.format("%.100f", d) return "2.3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"?
Format specifiers are described under the API for java.util.Formatter. The part about what the ".100" part does is here:
The width is the minimum number of characters to be written to the output. If the length of the converted value is less than the width then the output will be padded by ' ' (\u0020') until the total number of characters equals the width. The padding is on the left by default. If the '-' flag is given, then the padding will be on the right.
Basically, all we need to get from this is that padding does not add any actual mathematical precision to the calculation - it just adds zeroes to what you would have gotten without the padding. Since the println(d) prints "2.3", all we get with padding is to add zeroes to it.
This shows the possible double values in the neighborhood of 2.3:
When we write "2.3" that gets translated into the double closest to 2.3, which is on the middle line. When that value is printed, it is printed with the minimum number of digits after the decimal point which will still uniquely identify that value. And so "2.3" is sufficient, because no other double is closer to 2.3 than the value shown in the middle line.