rangga_hrdme

automatic result: System.out.println()

Apr 4th, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. // import java.lang.Math; // Activate if needed
  2. import java.time.LocalDateTime;
  3. import java.util.ArrayList;
  4. import java.util.Locale;
  5.  
  6. public class str_format {
  7.  
  8.     public static void main(String[] args) {
  9.         // String format: %[parameter_index$][flags][width][.precision]conversion_target
  10.  
  11.         String name = "tony stark";
  12.         String city = "bogor";
  13.         String movie = "avengers";
  14.         String job = "worker";
  15.  
  16.         ArrayList<String> index = new ArrayList<String>(); // CREATE ARRAY
  17.  
  18.         // EXAMPLE: 1
  19.         index.add(String.format("name is %s, live in %S", name, city));
  20.         index.add(String.format("name is %1$s, live in %2$S watch %3$s job: %4$s %n", name, city, movie, job)); // POSITION (index)
  21.         index.add(String.format("To be integer %40d others way", (int) Math.round(101.99)));
  22.         index.add(String.format("To be integer %,d others way", -1010987654));
  23.         index.add(String.format(Locale.ITALY, "To be integer %,d local", 1010987654));
  24.         index.add(String.format(Locale.ITALY, "To be precision %,.3f local", -10109.87654)); // PRECISION
  25.         index.add(String.format(Locale.ITALY, "To be zero float %,.0f local", -10109.87654));
  26.         index.add(String.format("value is %f", 32.33434));
  27.         index.add(String.format("value is %32.12f", 32.33434)); //returns 12 char fractional part filling with 0  
  28.  
  29.         // EXAMPLE: 2
  30.         index.add(String.format("Integer %d", 101));
  31.         index.add(String.format("String %s", "great person: Muhammad"));
  32.         index.add(String.format("Float %,f", 1_000_101.00));
  33.         index.add(String.format("Hexadecimal %x", 101));
  34.         index.add(String.format("Char %c", 'c'));
  35.  
  36.         // EXAMPLE: 3
  37.         index.add(String.format("%d", 101));
  38.         index.add(String.format("|%10d|", 101)); // Specifying length of integer  
  39.         index.add(String.format("|%-10d|", 101)); // Left-justifying within the specified width  
  40.         index.add(String.format("|% d|", 101));
  41.         index.add(String.format("|%010d|", 101)); // Filling with zeroes
  42.         index.add(String.format("To be integer %d", (int) 101.99));
  43.         index.add(String.format("To be integer %d other way", (int) Math.round(101.99)));
  44.         index.add(String.format("To be integer %40d others way", (int) Math.round(101.99))); // flags
  45.  
  46.         // DATE TIME EXAMPLE
  47.         LocalDateTime today = LocalDateTime.now();
  48.         index.add(String.format("Today is %tA", today));
  49.         index.add(String.format("Full date is %1$tA year %1$tY month %1$tm date %1$tm", today));
  50.         index.add(String.format("short date is %1$td.%1$tm.%1$ty %n", today));
  51.         index.add(String.format("Time is hours %tH: minutes %tM: seconds %tS %n", today, today, today));
  52.  
  53.         for (String result : index) {
  54.             System.out.println(result);
  55.         }
  56.  
  57.     }
  58. }
  59.  
Add Comment
Please, Sign In to add comment