⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return sign * r * Math.pow(10, e);
         }
         else return sign * r;
         i++;
      }
      return sign * r;
   }

   /**
   * Formats a double into a string (like sprintf in C)
   * @param x the number to format
   * @return the formatted string
   * @exception IllegalArgumentException if bad argument
   */

   public String form(double x)
   {  String r;
      if (precision < 0) precision = 6;
      int s = 1;
      if (x < 0) { x = -x; s = -1; }
      if (fmt == 'f')
         r = fixed_format(x);
      else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')
         r = exp_format(x);
      else throw new java.lang.IllegalArgumentException();

      return pad(sign(s, r));
   }

   /**
   * Formats a long integer into a string (like sprintf in C)
   * @param x the number to format
   * @return the formatted string
   */

   public String form(long x)
   {  String r;
      int s = 0;
      if (fmt == 'd' || fmt == 'i')
      {  s = 1;
         if (x < 0) { x = -x; s = -1; }
         r = "" + x;
      }
      else if (fmt == 'o')
         r = convert(x, 3, 7, "01234567");
      else if (fmt == 'x')
         r = convert(x, 4, 15, "0123456789abcdef");
      else if (fmt == 'X')
         r = convert(x, 4, 15, "0123456789ABCDEF");
      else throw new java.lang.IllegalArgumentException();

      return pad(sign(s, r));
   }

   /**
   * Formats a character into a string (like sprintf in C)
   * @param x the value to format
   * @return the formatted string
   */

   public String form(char c)
   {  if (fmt != 'c')
         throw new java.lang.IllegalArgumentException();

      String r = "" + c;
      return pad(r);
   }

   /**
   * Formats a string into a larger string (like sprintf in C)
   * @param x the value to format
   * @return the formatted string
   */

   public String form(String s)
   {  if (fmt != 's')
         throw new java.lang.IllegalArgumentException();
      if (precision >= 0) s = s.substring(0, precision);
      return pad(s);
   }


   /**
   * a test stub for the format class
   */

   public static void main(String[] a)
   {  double x = 1.23456789012;
      double y = 123;
      double z = 1.2345e30;
      double w = 1.02;
      double u = 1.234e-5;
      int d = 0xCAFE;

      double aa = 1.99;
      Format.print(System.out, "aa = |%5.2f|\n", aa);
      Format.print(System.out, "aa = |%5.1f|\n", aa);
      Format.print(System.out, "aa = |%5.0f|\n", aa);

      double bb = -117.6846;
      Format.print(System.out, "bb = |%5.4f|\n", bb);
      Format.print(System.out, "bb = |%5.3f|\n", bb);
      Format.print(System.out, "bb = |%5.2f|\n", bb);
      Format.print(System.out, "bb = |%5.1f|\n", bb);
      Format.print(System.out, "bb = |%5.0f|\n", bb);

      double cc = 117.05555;
      Format.print(System.out, "bb = |%5.4f|\n", cc);   //bb = |117.0555| doesn't round correctly!
      Format.print(System.out, "bb = |%5.3f|\n", cc);
      Format.print(System.out, "bb = |%5.2f|\n", cc);
      Format.print(System.out, "bb = |%5.1f|\n", cc);

      Format.print(System.out, "bb = |%5.0f|\n", bb);
      Format.print(System.out, "x = |%f|\n", x);
      Format.print(System.out, "u = |%20f|\n", u);
      Format.print(System.out, "x = |% .5f|\n", x);
      Format.print(System.out, "w = |%20.5f|\n", w);
      Format.print(System.out, "x = |%020.5f|\n", x);
      Format.print(System.out, "x = |%+20.5f|\n", x);
      Format.print(System.out, "x = |%+020.5f|\n", x);
      Format.print(System.out, "x = |% 020.5f|\n", x);
      Format.print(System.out, "y = |%#+20.5f|\n", y);
      Format.print(System.out, "y = |%-+20.5f|\n", y);
      Format.print(System.out, "z = |%20.5f|\n", z);

      Format.print(System.out, "x = |%e|\n", x);
      Format.print(System.out, "u = |%20e|\n", u);
      Format.print(System.out, "x = |% .5e|\n", x);
      Format.print(System.out, "w = |%20.5e|\n", w);
      Format.print(System.out, "x = |%020.5e|\n", x);
      Format.print(System.out, "x = |%+20.5e|\n", x);
      Format.print(System.out, "x = |%+020.5e|\n", x);
      Format.print(System.out, "x = |% 020.5e|\n", x);
      Format.print(System.out, "y = |%#+20.5e|\n", y);
      Format.print(System.out, "y = |%-+20.5e|\n", y);

      Format.print(System.out, "x = |%g|\n", x);
      Format.print(System.out, "z = |%g|\n", z);
      Format.print(System.out, "w = |%g|\n", w);
      Format.print(System.out, "u = |%g|\n", u);
      Format.print(System.out, "y = |%.2g|\n", y);
      Format.print(System.out, "y = |%#.2g|\n", y);

      Format.print(System.out, "d = |%d|\n", d);
      Format.print(System.out, "d = |%20d|\n", d);
      Format.print(System.out, "d = |%020d|\n", d);
      Format.print(System.out, "d = |%+20d|\n", d);
      Format.print(System.out, "d = |% 020d|\n", d);
      Format.print(System.out, "d = |%-20d|\n", d);
      Format.print(System.out, "d = |%20.8d|\n", d);
      Format.print(System.out, "d = |%x|\n", d);
      Format.print(System.out, "d = |%20X|\n", d);
      Format.print(System.out, "d = |%#20x|\n", d);
      Format.print(System.out, "d = |%020X|\n", d);
      Format.print(System.out, "d = |%20.8x|\n", d);
      Format.print(System.out, "d = |%o|\n", d);
      Format.print(System.out, "d = |%020o|\n", d);
      Format.print(System.out, "d = |%#20o|\n", d);
      Format.print(System.out, "d = |%#020o|\n", d);
      Format.print(System.out, "d = |%20.12o|\n", d);

      Format.print(System.out, "s = |%-20s|\n", "Hello");
      Format.print(System.out, "s = |%-20c|\n", '!');
      Format.print(System.out, "s = |%-3s|\n", "Hello");

      Format.print(System.out, "s = |%3s|\n", "Hello");
//      Format.print(System.out, "s = |%3c|\n", "Hello");  // causes error

   }

   private static String repeat(char c, int n)
   {  if (n <= 0) return "";
      StringBuffer s = new StringBuffer(n);
      for (int i = 0; i < n; i++) s.append(c);
      return s.toString();
   }

   private static String convert(long x, int n, int m, String d)
   {  if (x == 0) return "0";
      String r = "";
      while (x != 0)
      {  r = d.charAt((int)(x & m)) + r;
         x = x >>> n;
      }
      return r;
   }

   private String pad(String r)
   {  String p = repeat(' ', width - r.length());
      if (left_align) return pre + r + p + post;
      else return pre + p + r + post;
   }

   private String sign(int s, String r)
   {  String p = "";
      if (s < 0) p = "-";
      else if (s > 0)
      {  if (show_plus) p = "+";
         else if (show_space) p = " ";
      }
      else
      {  if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0') p = "0";
         else if (fmt == 'x' && alternate) p = "0x";
         else if (fmt == 'X' && alternate) p = "0X";
      }
      int w = 0;
      if (leading_zeroes)
         w = width;
      else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o')
         && precision > 0) w = precision;

      return p + repeat('0', w - p.length() - r.length()) + r;
   }

    /*
     * DDG - fixed rounding bug 01/05/2000
     * Before it would turn 1.99 -> "1.0" rathen then "2.0"
     */
   private String fixed_format(double d)
   {  String f = "";

      if (d > 0x7FFFFFFFFFFFFFFFL) return exp_format(d);

      // if 0 precision do rounding correctly
      //DDG      long l = (long)(precision == 0 ? d + 0.5 : d);

      double rnd = 0.5/Math.pow(10.0, precision);

      long l = (long) (d + rnd);

      //    System.out.println ("precision= "+ precision+ " rnd = "+rnd+ " l= "+l);

      f = f + l;

      double fr = d - (long) d;// fractional part

      if (fr >= 1 || fr < 0) return exp_format(d);

      return f + frac_part(fr);
   }

   private String frac_part(double fr)
   // precondition: 0 <= fr < 1
   {  String z = "";

      if (precision > 0)
      {  double factor = 1;
         String leading_zeroes = "";
         for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++)
         {  factor *= 10;
            leading_zeroes = leading_zeroes + "0";
         }
         long l = (long) (factor * fr + 0.5);

         z = leading_zeroes + l;
         z = z.substring(z.length() - precision, z.length());

      }


      if (precision > 0 || alternate) z = "." + z;
      if ((fmt == 'G' || fmt == 'g') && !alternate)
      // remove trailing zeroes and decimal point
      {  int t = z.length() - 1;
         while (t >= 0 && z.charAt(t) == '0') t--;
         if (t >= 0 && z.charAt(t) == '.') t--;
         z = z.substring(0, t + 1);
      }

      return z;
   }

   private String exp_format(double d)
   {  String f = "";
      int e = 0;
      double dd = d;
      double factor = 1;
      while (dd > 10) { e++; factor /= 10; dd = dd / 10; }
      while (dd < 1) { e--; factor *= 10; dd = dd * 10; }
      if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision)
         return fixed_format(d);

      d = d * factor;
      f = f + fixed_format(d);

      if (fmt == 'e' || fmt == 'g')
         f = f + "e";
      else
         f = f + "E";

      String p = "000";
      if (e >= 0)
      {  f = f + "+";
         p = p + e;
      }
      else
      {  f = f + "-";
         p = p + (-e);
      }

      return f + p.substring(p.length() - 3, p.length());
   }

   private int width;
   private int precision;
   private String pre;
   private String post;
   private boolean leading_zeroes;
   private boolean show_plus;
   private boolean alternate;
   private boolean show_space;
   private boolean left_align;
   private char fmt; // one of cdeEfgGiosxXos
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -