Advertisement
xellscream

FormatingNumbersJavaHW

May 19th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.text.Format;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class FormatingNumbers {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.  
  10.         double a = input.nextDouble();
  11.         double b = input.nextDouble();
  12.         double c = input.nextDouble();
  13.        
  14.         System.out.print("|" + convertHex(254));
  15.         System.out.print("|" + convertBin(254));
  16.        
  17.         System.out.printf("%10.2f|%-10.0f", b, c);
  18.     }
  19.  
  20.     public static String convertHex(int n) {
  21.         String converted = Integer.toHexString(n);
  22.         int difference = 10 - converted.length();
  23.         String current = converted;
  24.        
  25.         for (int i = 0; i < difference; i++) {
  26.             current += " ";
  27.         }
  28.        
  29.         return current;
  30.     }
  31.  
  32.     public static String convertBin(int n) {
  33.         String converted = Integer.toBinaryString(n);
  34.         int difference = 10 - converted.length();
  35.         String current = "";
  36.        
  37.         for (int i = 0; i < difference; i++) {
  38.             current += "0";
  39.         }
  40.        
  41.         current += converted;
  42.         return current;
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement