This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/5/29

int with leading zeros

erdelf erdelf

2012/5/29

#
hello, my problem is that I want an int with leading zeros, e.g. 0065 instead of 65. I found a code for this. i can be any number.
            
            String formatted = String.format("%04d", i);
            System.out.println(formatted);
my problem is that this code makes a string so I made this:
     
            String formatted = String.format("%04d", i);
            i = Integer.valueOf(formatted);
            System.out.println(i);      
Here is that the zeros are away. Any suggestions? (I need the variable i later so it is important that this is an int)
davmac davmac

2012/5/29

#
Best solution is probably just to do the conversion to a 4 digit string whenever you want to display the number. An 'int' can't store leading 0 digits; it's just a numerical value. Otherwise, keep two variables - one as a String and one as an int.
erdelf erdelf

2012/5/29

#
thx. I think I will use it-
You need to login to post a reply.