Java: double: how to ALWAYS show two decimal digits
You can simply do this:
double d = yourDoubleValue;
String formattedData = String.format("%.02f", d);
Use the java.text.NumberFormat
for this:
NumberFormat nf= NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
System.out.print(nf.format(decimalNumber));
You can use something like this:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.00");
System.out.print(df.format(d));
Edited to actually answer the question because I needed the real answer and this came up on google and someone marked it as the answer despite the fact that this wasn't going to work when the decimals were 0.