Advertisement
dimipan80

Formatting Numbers

Aug 17th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. /* Write a program that reads 3 numbers: an integer a (0 ≤ a ≤ 500),
  2.  * a floating-point b and a floating-point c
  3.  * and prints them in 4 virtual columns on the console.
  4.  * Each column should have a width of 10 characters.
  5.  * The number a should be printed in hexadecimal, left aligned;
  6.  * then the number a should be printed in binary form, padded with zeroes,
  7.  * then the number b should be printed with 2 digits after the decimal point, right aligned;
  8.  * the number c should be printed with 3 digits after the decimal point, left aligned. */
  9.  
  10. import java.util.Locale;
  11. import java.util.Scanner;
  12.  
  13. public class _06_FormattingNumbers {
  14.  
  15.     public static void main(String[] args) {
  16.         // TODO Auto-generated method stub
  17.         Locale.setDefault(Locale.ROOT);
  18.         Scanner scan = new Scanner(System.in);
  19.         System.out.print("Enter a Integer number in the range [0 ... 500] for numA: ");
  20.         int numA = scan.nextInt();
  21.  
  22.         if (numA >= 0 && numA <= 500) {
  23.             System.out.println("Enter 2 Real numbers for numB and numC, separated by a space:");
  24.             double numB = scan.nextDouble();
  25.             double numC = scan.nextDouble();
  26.  
  27.             String hexadecimalStr = Integer.toHexString(numA).toUpperCase();
  28.             System.out.printf("|%-10s|", hexadecimalStr);
  29.  
  30.             String binaryStr = Integer.toBinaryString(numA);
  31.             int binaryNum = Integer.parseInt(binaryStr);
  32.             System.out.printf("%010d|", binaryNum);
  33.  
  34.             System.out.printf("%10.2f|", numB);
  35.  
  36.             System.out.printf("%-10.3f|\n", numC);
  37.         } else {
  38.             System.out.println("Error! - The numA is Out of Range!!!");
  39.         }
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement