This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Adding a leading zero to a large string in Java
(2 answers)
Closed 8 years ago.
I'm using an int variable:
month = dp.getMonth() + 1;
currently getting an output of "2" and when I do the following:
if (month<10){
month = '0'+month;
};
I get: 50.
Your problem is that your '0' char is being coerced to an integer. Since '0' has an ASCII value of 48, you're getting 48 + 2 = 50.
Note that what you're trying to do won't work - you can't add a leading 0 to month, as month is a number. A leading zero only makes sense in a string representation of a number.
As explained in this answer, here's how to produce a zero-padded number:
String.format("%02d", month);
Related
This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 1 year ago.
I need to generate a sequence as follows:
PAY000000 - The first three characters(PAY) remain the same and the 000000 should be incremented by one:
I have tried the following method to generate a sequence number:
public String generateSequence(String currentPayment) {
String chunkNumeric = currentPayment.substring(3, 9);
return "PAY" + (Integer.parseInt(chunkNumeric) + 1);
}
Expected:
currentPayment: PAY000000 Expected value: PAY000001
currentPayment: PAY000001 Expected value: PAY000002
Actual Result:
currentPayment: PAY000001 Actual value: PAY2
The issue is when I pass PAY000001 as parameter the Integer.parseInt(chunkNumeric) remove all the leading zeros that is PAY2 generated instead of PAY000002.
Any idea how I can increment the string while keeping the leading zero?
You should instead maintain the sequence as a number, i.e. an integer or long, and then format that number with left padded zeroes:
public String generateSequence(int paymentSeq) {
return "PAY" + String.format("%06d", paymentSeq);
}
int seq = 1;
String nextSeq = generateSequence(seq);
This question already has answers here:
Convert hex string to int
(9 answers)
Closed 2 years ago.
I need to convert hexadecimal value which is OX12 to decimal in JAVA,
as I knew hexadecimal is based 16 (1..9, a..f), but in this case, I don't know how to convert with that value.
Can someone help me?
Thanks so much
If the number is a String, you can use the parseInt method of the Integer class with first argument "12" (the number in hex) and second argument 16, the radix, or base, of the number
int number = Integer.parseInt("12", 16);
System.out.println(number);
// output is 18
If the number is not a String, you can simply do the following:
int number = 0x12;
System.out.println(number);
// output is 18
This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.
This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();
This question already has answers here:
Left padding a String with Zeros [duplicate]
(20 answers)
Closed 8 years ago.
How to add zero as prefix if value less than equals to 9, i am using below way of achieving this:
int countZero = 0;
if(countVat <= 9)
{
countVat = countZero + countVat;
Log.d("countVat:", String.valueOf(countVat));
}
but this not works for me, still getting single digit if countVat value less than equals to 9.
Use String.format
String.format("%02d", num);