If you were to print out the String
returned by df.format(sum)
, then you would get:
2
Presumably you're printing the double
returned by your method, which will always print at least one decimal place:
2.0
The numbers themselves don't have any intrinsic formatting. To control the formatting, use df.format(sum)
and print the String
. There no need to parse it back to a double
, which would intentionally lose the formatting you just applied.