Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Format Doubt

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here in line1 4 zeroes are padded. But line2 only 3.

In case of decimal, isnt + sign counted as part of the width?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi vijay wrote:

here in line1 4 zeroes are padded. But line2 only 3.

In case of decimal, isnt + sign counted as part of the width?


You are completely wrong here Abhi, in line1 output is:
+12.340000


here no zero padded, as zero padding is done on left side not right side, by default %f shows 6 fractional digits, means 6 digits after a decimal point, so 8 place is already exhausted by %f, so no need to pad any zero on the left side.

In second case it is %d so it will show only +1234 that already took 5 places as + is counted in width, so %+08d will pad only 3 zeros on the left side.

Now run this one:


Remember +,(,.,- all are considered in the width.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Punit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic