I want to format a decimal number like this:
1234.5
0.5
It mean the max number would be 9999.9 (4 characters before ".", 1 character after)
I want to format a decimal number like this:
1234.5
0.5
It mean the max number would be 9999.9 (4 characters before ".", 1 character after)
Use DecimalFormat
:
DecimalFormat df = new DecimalFormat("###0.0");
String str = df.format(yourDecimal);
If you want to print your result, it may be simpler to use printf
for (double d : new double[]{1234.51111, 0.501})
System.out.printf("%6.1f%n", d);
prints
1234.5
0.5