How to leave-pad integer with spaces?

How to format an integer value without leading zero? I tried to use decimal format, but I can not achieve the exact result:

DecimalFormat dFormat=new DecimalFormat("000"); mTotalNoCallsLbl.setText(""+dFormat.format(mTotalNoOfCallsCount)); mTotalNoOfSmsLbl.setText(""+dFormat.format(mTotalNoOfSmsCount)); mSelectedNoOfCallsLbl.setText(""+dFormat.format(mSelectedNoOfCallLogsCount)); mSelectedNoOfSmsLbl.setText(""+dFormat.format(mSelectedNoOfSmsCount)); 

I get this output:

 500 004 011 234 

but I want:

 500 4 11 234 

My question is how to replace zeros with spaces?

+5
source share
1 answer

It looks like you want the left panel with spaces, so maybe you want:

 String.format("%3d", yourInteger); 

For instance:

 int[] values = { 500, 4, 11, 234 }; for (int v : values) { System.out.println(String.format("%3d", v)); } 

Conclusion:

  500
   4
  eleven
 234
+14
source

Source: https://habr.com/ru/post/1203193/


All Articles