I want to format a double with a certain number of decimal places in Java. Currently my code is as follows:
final NumberFormat format = NumberFormat.getInstance();
format.setMinimumFractionDigits(decimalPlaces);
format.setMaximumFractionDigits(decimalPlaces);
format.setGroupingUsed(false);
String s = format.format(value);
but when decimalPlacesgreater than 340, this method simply prints 340 digits and ignores the rest. Is there a way around this limitation?
In particular, in my case they are all 0s (and probably will be with any number after a certain point, since the accuracy is not so high), but it seems wrong to me that the function just silently ignores what I want, and does not throw an exception or something else.
Yes, I know that double cannot actually hold so many decimal places, but it is also true that int cannot contain any decimal places, but I can print it with some.
source
share