double A= 0.1, B= -0.1;

DecimalFormat numform = new DecimalFormat(" 000.000;-000.000"); 

System.out.println( "A = " + numform.format(A) );
System.out.println( "B = " + numform.format(B) );

Answer:

A =  000.100
B = -000.100

Collapsing Zeros

The output is somewhat unsatisfactory. It would look better without the leading and trailing zeros

Use the character # in the format string to show a digit that will be omitted from the output string if it is a leading or trailing zero.

For integers: the pattern has two halves. It starts with any number of #s, followed by one or more 0s. Here are some legal patterns:

####0 
###00 
##0 
000
###0

For floating point: The integer part of the pattern follows the rules for integers. If a decimal point is included, the pattern for the fraction has two halves: it starts with any number of 0s followed by any number of #s. Here are some legal patterns:

####0.##
###00.##
##0.00##
##0.000
000
###0
##0.

Here are some illegal patterns:

####        ---  needs to end with at least one 0
###.##      ---  integer part needs to end with a 0
##0.0##00   ---  bad fractional part (can't surround #s with 0s)
##00##      ---  bad integer pattern
###0.##0    ---  bad fractional part

Usually format() produces a reasonable string, even with defective format patterns. But seriously defective patterns cause it to throw an IllegalArgumentException at run time. The compiler does not inspect the format patterns.

QUESTION 15:

Is the following format string correct?

000.###