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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java number formatting

  • 03-03-2006 6:47pm
    #1
    Registered Users, Registered Users 2 Posts: 4,225 ✭✭✭


    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