| Gandalf| |Gandalf |
public class ZeroPad { public static void main ( String[] args ) { double x = 12.34, y = -6.95, z = 1024; System.out.printf("x:%8.3f, y:%8.3f, z:%8.3f%n", x, y, z); // right just; blank padding System.out.printf("x:%08.3f, y:%08.3f, z:%08.3f%n%n", x, y, z); // right just; zero padding int a = 12, b = 12345, c = -1234567; System.out.printf("a:%10d; b:%10d; c:%10d%n", a, b, c); // right just; blank padding System.out.printf("a:%010d; b:%010d; c:%010d%n", a, b, c); // right just; zero padding } }
To pad on the left with zeros instead of spaces, insert a "0" in front of the field width. (This only works with numeric types.) Sometimes you see this done with financial documents. The above program prints:
x: 12.340, y: -6.950, z:1024.000 x:0012.340, y:-006.950, z:1024.000 a: 12; b: 12345; c: -1234567 a:0000000012; b:0000012345; c:-001234567
Do you think you can pad on the right with zeros?