java Q: how do i turn int "76" into "0076" ?

Discussion in 'OT Technology' started by sushibug, Oct 19, 2006.

  1. sushibug

    sushibug :happysad:

    Joined:
    May 16, 2002
    Messages:
    3,096
    Likes Received:
    0
    Location:
    San Diego, CA
    i tried googling this but that wasnt successful.
    the teacher never told us how to do this either.

    a little help.
     
  2. k2737

    k2737 Active Member

    Joined:
    Nov 29, 2004
    Messages:
    3,324
    Likes Received:
    0
    Location:
    Ft Lauderdale
    Convert it to a string then append the zeros, then change it back to an int? Ghetto but works.
     
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    i know in c++ if you can do

    printf("%04d", myint);

    the 4 says use 4 spaces to print the number and the 0 in front makes it use 0's instead of spaces if the number doesnt take up 4 spaces. java blows :)
     
  4. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
  5. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    read up on java.util.DecimalFormat
    i'm pretty sure if you do
    Code:
    DecimalFormat dec = new DecimalFormat(####.00);
    System.out.println(dec.format(whatevervariable));
    
    it will work
     

Share This Page