19

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.

0

1 Answer 1

70

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.