Advertisement
viraldim

FormattingNumbers

May 13th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. /*6.Write a program that reads 3 numbers: an integer a (0 ≤ a ≤ 500), a floating-point
  2. b and a floating-point c and prints them in 4 virtual columns on the console.
  3. Each column should have a width of 10 characters. The number a should be printed
  4. in hexadecimal, left aligned; then the number a should be printed in binary form,
  5. padded with zeroes, then the number b should be printed with 2 digits after the
  6. decimal point, right aligned; the number c should be printed with 3 digits after
  7. the decimal point, left aligned.*/
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class FormattingNumbers {
  12.  
  13.     public static void main(String[] args) {
  14.        
  15.         Scanner input = new Scanner(System.in);
  16.         System.out.println("Enter a positive integer number!");
  17.         int num =input.nextInt();
  18.         System.out.println("Enter a float number!");
  19.         float numF1 = input.nextFloat();
  20.         System.out.println("Enter a float number!");
  21.         float numF2 = input.nextFloat();
  22.         String hexNum = Integer.toHexString(num);
  23.         String binNum = Integer.toBinaryString(num);
  24.  
  25.         System.out.format("|%-10s|%010d|%10.2f|%-10.3f|", hexNum, Integer.parseInt(binNum),numF1,numF2);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement