Click to See Complete Forum and Search --> : adding digits to an int


sae
10-03-2006, 02:22 PM
How can I change a one digit integer (i.e. 5, 6, etc) to a two digit integer (i.e. 05, 06, etc)

Khalid Ali
10-03-2006, 03:36 PM
preceding zero does not hold a value hence its not shown when data type is int, however, to show that you will need to change the data type to string.
int n=5;
//desired result =05
//work around
String strN = (n<10)?("0"+n):""+n;

see if this works...

sae
10-03-2006, 07:58 PM
never thought about converting to a string. of course do do calculations I would have to convert back to an integer, but that's easy enough.

agent_x91
10-09-2006, 06:07 AM
What you're decribing is usually used to fix single-digit values before displaying a date or time, so that the result is something like 09:02 am rather than 9:2 am. Normally I'd just use a quick method like this:


private String fixNumber(int num)
{
if(num<10)
return "0"+num;

return num+"";
}