Still i am getting file name like below:
B-13-4-006.jpg
and
B-5-7-008.jpg
but now i want to show 0 as prefix if value less than < 9 else as it is, in short want to show values in double figures, see my code below using to file name
"B-" + // prefix
LoginActivity.strEventID + "-" + // eventID
LoginActivity.strOperativeID + "-" + // operativeID
getNextNumber() + // counter
".jpg"
but i require , files name should look like below:
B-13-04-006.jpg
and
B-05-07-008.jpg
Use String.format("%02d", yournumber);
to show 0 with a number if less than 10 (for two digits number).
Use method like
private String getPaddedNumber(int number) {
return String.format("%02d", number);
}
You can read Formatter documents for more details.
How to use into your code
"B-" + // prefix
getPaddedNumber(LoginActivity.strEventID) + "-" + // eventID
getPaddedNumber(LoginActivity.strOperativeID) + "-" + // operativeID
getPaddedNumber(getNextNumber()) + // counter
".jpg"
As you seem to have strings that need to be (optionally) padded with zeros, you can use a different approach than generally used to pad integers :
public String addPadding(int length, String text) {
StringBuilder sb = new StringBuilder();
// First, add (length - 'length of text') number of '0'
for (int i = length - text.length(); i > 0; i--) {
sb.append('0');
}
// Next, add string itself
sb.append(text);
return sb.toString();
}
so you can use:
"B-" + // prefix
addPadding(2, LoginActivity.strEventID) + "-" + // eventID
addPadding(2, LoginActivity.strOperativeID) + "-" + // operativeID
getNextNumber() + // counter
".jpg"
There are lots of other possibilities to pad a String, see this question for more details/possibilities.
public static String convert(int n){
return n < 10 ? "0" + n : "" + n;
}
Just use a formatter. I think this is the easiest and most accurate approach
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.