Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

formatting

 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.......i have doubt in formatting

int i1 = -123;
int i2 = 12345;

System.out.printf(">%0,7d< \n", i2);
System.out.format(">%+-7d< \n", i2);

result is

>012,345<
>+12345 <

can you explain me this......how did the comma go between 012 and y the - symbol was not inserted....

thanks...
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The - symbol was not printed since you are printing a positive integer (i2 is greater than zero)
and when the comma symbol is used to format the numbers, typically comma separates group of 3 digits from the RHS. Thus, 1234567 will be printed as 1,234,567
 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply...but i din get the concept of i2 greater than zero....
 
Rodolfo Velasco
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the - symbol , if you're printing a negative number .. it'll be printed inside parenthesys



result is

>(5355)<

Hope this helps...
 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that happens when you use "(" this symbol..... - will jus insert - i guess
 
Rodolfo Velasco
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oo.. thanks..
is good to know..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic