Approach 1: Just append 0 to the number.
private static void printNumberInTwoDigits_1(int num) {
System.out.println("0" + num);
}
Approach 2: Using String.format
private static void printNumberInTwoDigits_2(int num) {
String result = String.format("%02d", num);
System.out.println(result);
}
Approach 3: Using System.out.printf.
private static void printNumberInTwoDigits_3(int num) {
System.out.printf("%02d\n", num);
}
Find the below working application.
package com.sample.app;
public class App {
private static void printNumberInTwoDigits_1(int num) {
System.out.println("0" + num);
}
private static void printNumberInTwoDigits_2(int num) {
String result = String.format("%02d", num);
System.out.println(result);
}
private static void printNumberInTwoDigits_3(int num) {
System.out.printf("%02d\n", num);
}
public static void main(String args[]) {
printNumberInTwoDigits_1(9);
printNumberInTwoDigits_2(9);
printNumberInTwoDigits_3(9);
}
}
Output
09
09
09
You may
like
No comments:
Post a Comment