Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java number formatting

  • 03-03-2006 07:47PM
    #1
    Registered Users, Registered Users 2 Posts: 4,228 ✭✭✭


    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, Registered Users 2 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