src/share/classes/java/util/Formatter.java

Print this page




  33 import java.io.FileNotFoundException;
  34 import java.io.Flushable;
  35 import java.io.OutputStream;
  36 import java.io.OutputStreamWriter;
  37 import java.io.PrintStream;
  38 import java.io.UnsupportedEncodingException;
  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.nio.charset.IllegalCharsetNameException;
  45 import java.nio.charset.UnsupportedCharsetException;
  46 import java.text.DateFormatSymbols;
  47 import java.text.DecimalFormat;
  48 import java.text.DecimalFormatSymbols;
  49 import java.text.NumberFormat;
  50 import java.util.regex.Matcher;
  51 import java.util.regex.Pattern;
  52 
  53 import sun.misc.FpUtils;
  54 import sun.misc.DoubleConsts;
  55 import sun.misc.FormattedFloatingDecimal;
  56 
  57 /**
  58  * An interpreter for printf-style format strings.  This class provides support
  59  * for layout justification and alignment, common formats for numeric, string,
  60  * and date/time data, and locale-specific output.  Common Java types such as
  61  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  62  * are supported.  Limited formatting customization for arbitrary user types is
  63  * provided through the {@link Formattable} interface.
  64  *
  65  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  66  * safety is optional and is the responsibility of users of methods in this
  67  * class.
  68  *
  69  * <p> Formatted printing for the Java language is heavily inspired by C's
  70  * {@code printf}.  Although the format strings are similar to C, some
  71  * customizations have been made to accommodate the Java language and exploit
  72  * some of its features.  Also, Java formatting is more strict than C's; for
  73  * example, if a conversion is incompatible with a flag, an exception will be


3400                 = new char[v.length + prec - outPrec + (needDot ? 1 : 0)];
3401             System.arraycopy(v, 0, tmp, 0, v.length);
3402 
3403             // Add dot if previously determined to be necessary.
3404             int start = v.length;
3405             if (needDot) {
3406                 tmp[v.length] = '.';
3407                 start++;
3408             }
3409 
3410             // Add zeros.
3411             for (int j = start; j < tmp.length; j++)
3412                 tmp[j] = '0';
3413 
3414             return tmp;
3415         }
3416 
3417         // Method assumes that d > 0.
3418         private String hexDouble(double d, int prec) {
3419             // Let Double.toHexString handle simple cases
3420             if(!FpUtils.isFinite(d) || d == 0.0 || prec == 0 || prec >= 13)
3421                 // remove "0x"
3422                 return Double.toHexString(d).substring(2);
3423             else {
3424                 assert(prec >= 1 && prec <= 12);
3425 
3426                 int exponent  = Math.getExponent(d);
3427                 boolean subnormal
3428                     = (exponent == DoubleConsts.MIN_EXPONENT - 1);
3429 
3430                 // If this is subnormal input so normalize (could be faster to
3431                 // do as integer operation).
3432                 if (subnormal) {
3433                     scaleUp = Math.scalb(1.0, 54);
3434                     d *= scaleUp;
3435                     // Calculate the exponent.  This is not just exponent + 54
3436                     // since the former is not the normalized exponent.
3437                     exponent = Math.getExponent(d);
3438                     assert exponent >= DoubleConsts.MIN_EXPONENT &&
3439                         exponent <= DoubleConsts.MAX_EXPONENT: exponent;
3440                 }




  33 import java.io.FileNotFoundException;
  34 import java.io.Flushable;
  35 import java.io.OutputStream;
  36 import java.io.OutputStreamWriter;
  37 import java.io.PrintStream;
  38 import java.io.UnsupportedEncodingException;
  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.nio.charset.IllegalCharsetNameException;
  45 import java.nio.charset.UnsupportedCharsetException;
  46 import java.text.DateFormatSymbols;
  47 import java.text.DecimalFormat;
  48 import java.text.DecimalFormatSymbols;
  49 import java.text.NumberFormat;
  50 import java.util.regex.Matcher;
  51 import java.util.regex.Pattern;
  52 

  53 import sun.misc.DoubleConsts;
  54 import sun.misc.FormattedFloatingDecimal;
  55 
  56 /**
  57  * An interpreter for printf-style format strings.  This class provides support
  58  * for layout justification and alignment, common formats for numeric, string,
  59  * and date/time data, and locale-specific output.  Common Java types such as
  60  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  61  * are supported.  Limited formatting customization for arbitrary user types is
  62  * provided through the {@link Formattable} interface.
  63  *
  64  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  65  * safety is optional and is the responsibility of users of methods in this
  66  * class.
  67  *
  68  * <p> Formatted printing for the Java language is heavily inspired by C's
  69  * {@code printf}.  Although the format strings are similar to C, some
  70  * customizations have been made to accommodate the Java language and exploit
  71  * some of its features.  Also, Java formatting is more strict than C's; for
  72  * example, if a conversion is incompatible with a flag, an exception will be


3399                 = new char[v.length + prec - outPrec + (needDot ? 1 : 0)];
3400             System.arraycopy(v, 0, tmp, 0, v.length);
3401 
3402             // Add dot if previously determined to be necessary.
3403             int start = v.length;
3404             if (needDot) {
3405                 tmp[v.length] = '.';
3406                 start++;
3407             }
3408 
3409             // Add zeros.
3410             for (int j = start; j < tmp.length; j++)
3411                 tmp[j] = '0';
3412 
3413             return tmp;
3414         }
3415 
3416         // Method assumes that d > 0.
3417         private String hexDouble(double d, int prec) {
3418             // Let Double.toHexString handle simple cases
3419             if(!Double.isFinite(d) || d == 0.0 || prec == 0 || prec >= 13)
3420                 // remove "0x"
3421                 return Double.toHexString(d).substring(2);
3422             else {
3423                 assert(prec >= 1 && prec <= 12);
3424 
3425                 int exponent  = Math.getExponent(d);
3426                 boolean subnormal
3427                     = (exponent == DoubleConsts.MIN_EXPONENT - 1);
3428 
3429                 // If this is subnormal input so normalize (could be faster to
3430                 // do as integer operation).
3431                 if (subnormal) {
3432                     scaleUp = Math.scalb(1.0, 54);
3433                     d *= scaleUp;
3434                     // Calculate the exponent.  This is not just exponent + 54
3435                     // since the former is not the normalized exponent.
3436                     exponent = Math.getExponent(d);
3437                     assert exponent >= DoubleConsts.MIN_EXPONENT &&
3438                         exponent <= DoubleConsts.MAX_EXPONENT: exponent;
3439                 }