Java examples for Language Basics:Number Format
Formatting Numbers, align right, with decimal
import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner userInput = new Scanner(System.in); int a = userInput.nextInt(); double b = userInput.nextDouble(); double c = userInput.nextDouble(); String aHex = ""; String aBin = ""; if ((a >= 0) && (a <= 500)) { aHex = Integer.toHexString(a).toUpperCase(); aBin = String.format("%10s", Integer.toBinaryString(a)).replace(" ", "0"); } else {/*w w w . ja v a 2s.co m*/ System.out.println("Not in range!"); } System.out.printf("|%-10s|%s|%10.2f|%-10.3f|", aHex, aBin, b, c); } }