Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: add leading zero to an integer

  1. #1
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default add leading zero to an integer

    Hello all,

    This question is actually for an android app Im working on but the question is not android specific. I have a user enter numbers into an application. The numbers are limited to being two digits. If a user enters 1,2,3,4,5,6,7,8,9 than I would like to add a leading zero to the number such as 01,02,03....

    This is what I have so far:

    Java Code:
    if (currentNumber < 10) {
    					
         int length = String.valueOf(currentNumber).length();
    					
         if (length < 2) {
    						
              String formattedNumber = String.format("%02d", currentNumber);
              currentNumber = Integer.parseInt(formattedNumber);
         }
    }
    The String version of the number appears as 01 but when I parse it back to the number it goes in as just 1. I understand this is probably a limitation on integers but would prefer a solution (if exists) that does not involve changing the data types.

    When the user does enter 01, it is displayed and carried throughout the application as 01. Any help would be appreciated.

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: add leading zero to an integer

    when I parse it back to the number it goes in as just 1.
    How do you view the number to see if it has leading 0s? If an int is converted to a String, leading 0s are not shown. You need to define how many leading 0s you want when it is converted to a String like you are doing in format(). An int uses 32 bits to hold its value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: add leading zero to an integer

    Quote Originally Posted by Norm View Post
    How do you view the number to see if it has leading 0s? If an int is converted to a String, leading 0s are not shown. You need to define how many leading 0s you want when it is converted to a String like you are doing in format(). An int uses 32 bits to hold its value.
    I am viewing the value of the number through the debugger. The string representation of the number does show a leading zero after the format conversion. When I parse the number back to an integer it goes from 01 to 1. I only want one leading zero to be added "if" the user did not already enter a leading zero. I know I could display the number with a leading zero throughout the application by using the String.format or DecimalFormat but If I can change it at the beginning, it will alleviate a lot of unnecessary work.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: add leading zero to an integer

    When you convert any int value to a String for viewing, you will have to use a format to add leading 0s.
    Normal conversion of an int to a String does not include any leading 0s. An int is a primitive that does not include formatting information. It is up to the programmer to use format to add leading 0s when converting an int value to a String.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: add leading zero to an integer

    Ok, but is it even possible to add the leading zero to the integer so it will be stored as 0x? Worst case, I could use a switch statement with 9 cases and manually change the value but that seems kind of ridiculous. What are my other options, convert number to Integer?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: add leading zero to an integer

    An int uses 32 bits of memory to hold its value. A bit has a value of 0 or 1. If you were able to see an int in memory it could look like this: 00000000000010000000100000010000010
    As you can see, there are lots of leading 0s. Normally they all all stripped when an int is converted to a String.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: add leading zero to an integer

    Quote Originally Posted by generaltso View Post
    Ok, but is it even possible to add the leading zero to the integer so it will be stored as 0x?
    No. Then you're not talking about an integer anymore, you're talking about a String.

    Where you are mistaken here is that you don't differentiate DATA from the PRESENTATION. You want to store an integer as data so you may use it in calculations and what not, but when you want to display it, you want it to have a leading zero. The leading zero does not physically have to be in the data, you only want a leading zero to be printed when you display the number. See the distinction?

    And if you don't want to use the integer in calculations... then why do you want it to be an integer at all? A String containing numbers is enough then. Perhaps what you really want is to make sure that the String you have contains only numeric characters?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: add leading zero to an integer

    Bottom line -- how a integer or floating point number is stored in memory has nothing to do with how it is presented when printing out (which results in conversion to a string). Negative integers and longs are stored in 2's complement form. And consider the value 12.0 as a float. It is stored as 01000001010000000000000000000000. As an integer, this would be 1094713344. (See IEEE 754 for details).

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: add leading zero to an integer

    It's all a matter of convention: on earth, most of us represent numbers in decimal (fingers, toes) without leading zeros; that radix is arbitrary but the lack of leading zeros in the representation makes sense: how many leading zeros are appropriate? My 000000000000002 cents ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: add leading zero to an integer

    Thanks everyone for the help. I had already understood how to show the String representation of my number with leading zero. These numbers inputted by the user are passed around and displayed in a number of different places throughout the application and I was trying to get around not having to format the number every time it is displayed. I do need the number for calculations so storing as a String would not really work. It is a little frustrating because when the user enters 01 then the number is parsed from a String (edit text) and converted to integer where it is displayed and stored as 01 without any formatting and I assumed incorrectly that manually changing user input of 1 to 01 would be pretty simple. I understand why it doesn't work like that now but it took it had to sink in. Thanks again

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: add leading zero to an integer

    Depending on what those numbers represent, store them as part of a class representing whatever this data is.
    eg:
    Java Code:
    public SomeClass {
       private int theInt;
    
       public void setTheInt(int theInt) {
          this.theInt = theInt;
       }
    
       public int getTheInt() {
          return theInt;
       }
    
       public String getFormattedTheInt() {
          return ... code for formatting as you require ...
       }
    }
    That's how it's normally done.
    That way you only need one set of formatting code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 07:49 PM
  2. How to add leading zeroes to int
    By pink123 in forum New To Java
    Replies: 11
    Last Post: 03-01-2011, 07:18 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. integer with leading zero?!
    By tomiu in forum New To Java
    Replies: 4
    Last Post: 04-22-2009, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •