Results 1 to 12 of 12
  1. #1
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default How to add leading zeroes to int

    Hi,

    I have an int value Extension.It should be 4 diguts long always.Even if the data via the procedure comes 45 .It should treat it as 0045.
    I cannot convert it to string as my setter method is taking int as an argument.
    I get the value of extension from database as
    extension as

    int extension=rs.getint("extension")
    obj.setExtension(int)


    This extension should add zeroes if it is not 4 digits.

    Please help I am stuck up.

    Thanks,

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    After a quick google search of "java format int", I found these promising results:
    NumberFormat (Java 2 Platform SE v1.4.2)
    DecimalFormat (Java 2 Platform SE v1.4.2)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    The String.format function should help you do that.

    Java Code:
    String formattedValueString = String.format("Integer value: %04d", integerValue);
    The first parameter passed to the function is a string describes how to format whatever variables you want put into a string. In this case "Integer value: %04d" would create a string like "Integer Value: 0015" if integerValue was 15. The "%" indicates there should be a variable placed at that point, the following "0" indicates you want to pad the value with 0s (you can use a space instead to pad with spaces), the "4" indicates you want the value to be 4 characters long, and the "d" indicates the variable is an integer value. The variable used by the % corresponds to the variables passed to String.format after the first string, so the first % corresponds to the 2nd variable passed to String.format, the 2nd % to the 3rd variable and so on.

    There's a lot more you can do with the String.format so I recommend you look into the docs on it.

    Edit:
    Whoops I missed the part about not converting to a string. You can't actually add leading zeros to an integer value, you can only add the leading zeros when representing the value as a string because a CPU doesn't know the difference between 0045 and 45 as they are the exact same number, the number is only being represented differently.
    Last edited by Skiller; 03-01-2011 at 05:30 PM.

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

    Default

    An int does not have leading zeros.
    That's it.
    Full stop.

    Now, if you;re talking about displaying the int then you want to use a NumberFormat when you display it.

    ETA: Or, put another way, what he says up there...
    Getting interrupted mid-post is sooo annoying!

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

    Default

    To the OP, please don't post a question in multiple sub-forums...it's very annpying.

  6. #6
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    Thanks for your reply but Can you help me out with which method to use.I have read but couldnt find appropiate method with would add left leading zeroes to an int and return int

  7. #7
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    Thanks for the reply

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

    Default

    Quote Originally Posted by pink123 View Post
    Thanks for your reply but Can you help me out with which method to use.I have read but couldnt find appropiate method with would add left leading zeroes to an int and return int
    Which bit of "(a)n int does not have leading zeros" do you not understand?
    An int has no format (which is what you are talking about).

    What is your actual problem, because I suspect you've misunderstood some requirement somewhere along the line?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by pink123 View Post
    Thanks for your reply but Can you help me out with which method to use.I have read but couldnt find appropiate method with would add left leading zeroes to an int and return int
    That doesn't make sense. An int doesn't have leading zeroes, as you've been told numerous times now, so no method is going to add leading zeroes to an int and return an int. Why do you think you need to do this?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    Thanks for the reply.
    My problem is
    I have a variable which should have 4 digits always whether or not user enters 4 digits.
    When I fetch from the database
    Extension=rs.getInt("Extension");

    Now I want to set the value with zeroes added like if for eg the Extension=45.I should set in the setter
    obj.setExtension(0045).The setter can accept values of type int.

    Thanks,

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Sigh. I think I'm done trying to help you.

    You've been told several times now: ints, by themselves, do not have leading zeroes (unless you're talking about octal or hexadecimal values, which you aren't, and even those aren't leading zeroes).

    If you want to display leading zeroes, you use a formatter, which will almost definitely use Strings somehow.

    If you don't understand that, I can't help you further.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. help with saving bleeding zeroes.
    By SteroidalPsycho in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 09:55 AM
  2. Shift Off Trailing Zeroes
    By nwboy74 in forum New To Java
    Replies: 5
    Last Post: 02-25-2010, 08:56 AM
  3. integer with leading zero?!
    By tomiu in forum New To Java
    Replies: 4
    Last Post: 04-22-2009, 10:42 PM
  4. How to display numbers with leading zeros
    By Java Tip in forum java.lang
    Replies: 1
    Last Post: 06-14-2008, 07:36 PM
  5. How to display numbers with leading zeros
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:34 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
  •