Returns a string representation of number with at least 2 digits - Java java.util

Java examples for java.util:Date Format

Description

Returns a string representation of number with at least 2 digits

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int number = 2;
        System.out.println(printTwoDigits(number));
    }//from www .j  a va2s. c  o  m

    /**
     * Returns a string representation of number with at least 2 digits
     */
    private static String printTwoDigits(int number) {
        if (number > 9) {
            return String.valueOf(number);
        } else {
            return "0" + number;
        }
    }
}

Related Tutorials