Issue
Is there a difference between a format string with the $ included?
%1d vs %1$d
I noticed that in the app code we have both and both seems to work ok.
or by the way using %1d to %2d vs %1$d to %2$d
Solution
%d- just display number
int first = 10;
System.out.printf("%d", first);
// output:
// 10
%1d- display number with information how much space should be "reserved" (at least one)
int first = 10;
System.out.printf("->%1d<-", first);
// output:
// ->10<-
%9d- reserve "9" chars (if number will be shorter - put spaces there) and align to right
int first = 10;
System.out.printf("->%9d<-", first);
// output:
// -> 10<-
// 123456789
%-9d- same as above but align to LEFT (thisminusis important)
int first = 10;
System.out.printf("->%-9d<-", first);
// output:
// ->10 <-
// 123456789
%1$d- use (in format) position of the element from varargs (so you can REUSE elements instead of passing them two times)
int first = 10;
int second = 20;
System.out.printf("%1$d %2$d %1$d", first, second);
// output:
// 10 20 10
%1$9d- mix two of them. Get first parameter (1$) and reserve nine chars (%9d)
int first = 10;
System.out.printf("->%1$9d<-", first);
// output:
// -> 10<-
// 123456789
Answered By - Boken
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.