Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java number formatting

Options
  • 03-03-2006 7:47pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    yes it may be trivial but my head is too melted at this stage on a Friday evening to work this out. :(

    basically all i need it how to format an integer so that if its less than 10 it will have the 0 before the number

    e.g 1 to 01, 5 to 05 etc

    Thanks!


Comments

  • Closed Accounts Posts: 210 ✭✭deimos


    Scruff wrote:
    yes it may be trivial but my head is too melted at this stage on a Friday evening to work this out. :(

    basically all i need it how to format an integer so that if its less than 10 it will have the 0 before the number

    e.g 1 to 01, 5 to 05 etc

    Thanks!

    If you mean for printing it out on the screen I would use a conditional operator


    System.out.println("Number: " + (i > 10 ? "" : "0") + i);


  • Registered Users Posts: 1,272 ✭✭✭i_am_dogboy


    You'd want to use the NumberFormat class
    import java.text.NumberFormat;
    .
    .
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumIntegerDigits(2);
    .
    .
    System.out.println(nf.format(theNumber));
    


Advertisement