String formating

Format Flags: Space, + and (

format position of output text base of flags

s1= String.format("%d",123);
s2= String.format("%d",-123);
s3= String.format("% d",123);
s4= String.format("% d",-123);
s5= String.format("%+d",123);
s6= String.format("%+d",-123);
s7= String.format("%(d",123);
s8= String.format("%(d",-123);
s9= String.format("% (d",123);

s9 is using combined flags, this way positive value and negative value are align
output:

123
-123
 123
-123
+123
-123
123
(123)
 123

Argument Index

Not specified
% – start of format string
index$ – index of argument to use (4$ forth argument)
d – type of string ( d – decimal, s – string )
< corresponds to this same value as previous value

s1 = String.format("%d %d %d", 10, 20, 30);
10 20 30
        
s2 = String.format("%3$d %1$d %2$d", 10, 20, 30);
30 10 20
      
s3 = String.format("%3$d %<04d %1$d", 10, 20, 30);
30 0030 10



http://bit.ly/java8formatter

Leave a Reply

Your email address will not be published. Required fields are marked *