Formatting Numbers, align right, with decimal - Java Language Basics

Java examples for Language Basics:Number Format

Description

Formatting Numbers, align right, with decimal

Demo Code

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);

  }
}

Related Tutorials