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

Print this page
rev 7490 : 6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException
Summary: For zero value ensure than an unpadded zero character is passed to Formatter.addZeros()
Reviewed-by: TBD
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


3280                           FormattedFloatingDecimal.Form.DECIMAL_FLOAT);
3281 
3282                 char[] mant = addZeros(fd.getMantissa(), prec);
3283 
3284                 // If the precision is zero and the '#' flag is set, add the
3285                 // requested decimal point.
3286                 if (f.contains(Flags.ALTERNATE) && (prec == 0))
3287                     mant = addDot(mant);
3288 
3289                 int newW = width;
3290                 if (width != -1)
3291                     newW = adjustWidth(width, f, neg);
3292                 localizedMagnitude(sb, mant, f, newW, l);
3293             } else if (c == Conversion.GENERAL) {
3294                 int prec = precision;
3295                 if (precision == -1)
3296                     prec = 6;
3297                 else if (precision == 0)
3298                     prec = 1;
3299 








3300                 FormattedFloatingDecimal fd
3301                         = FormattedFloatingDecimal.valueOf(value, prec,
3302                           FormattedFloatingDecimal.Form.GENERAL);




3303 
3304                 char[] exp = fd.getExponent();
3305                 if (exp != null) {
3306                     prec -= 1;
3307                 } else {
3308                     prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1;
3309                 }
3310 
3311                 char[] mant = addZeros(fd.getMantissa(), prec);
3312                 // If the precision is zero and the '#' flag is set, add the
3313                 // requested decimal point.
3314                 if (f.contains(Flags.ALTERNATE) && (prec == 0))
3315                     mant = addDot(mant);
3316 
3317                 int newW = width;
3318                 if (width != -1) {
3319                     if (exp != null)
3320                         newW = adjustWidth(width - exp.length - 1, f, neg);
3321                     else
3322                         newW = adjustWidth(width, f, neg);
3323                 }
3324                 localizedMagnitude(sb, mant, f, newW, l);
3325 
3326                 if (exp != null) {
3327                     sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');
3328 
3329                     Flags flags = f.dup().remove(Flags.GROUP);
3330                     char sign = exp[0];
3331                     assert(sign == '+' || sign == '-');




3280                           FormattedFloatingDecimal.Form.DECIMAL_FLOAT);
3281 
3282                 char[] mant = addZeros(fd.getMantissa(), prec);
3283 
3284                 // If the precision is zero and the '#' flag is set, add the
3285                 // requested decimal point.
3286                 if (f.contains(Flags.ALTERNATE) && (prec == 0))
3287                     mant = addDot(mant);
3288 
3289                 int newW = width;
3290                 if (width != -1)
3291                     newW = adjustWidth(width, f, neg);
3292                 localizedMagnitude(sb, mant, f, newW, l);
3293             } else if (c == Conversion.GENERAL) {
3294                 int prec = precision;
3295                 if (precision == -1)
3296                     prec = 6;
3297                 else if (precision == 0)
3298                     prec = 1;
3299 
3300                 char[] exp;
3301                 char[] mant;
3302                 int expRounded;
3303                 if (value == 0.0) {
3304                     exp = null;
3305                     mant = new char[] {'0'};
3306                     expRounded = 0;
3307                 } else {
3308                     FormattedFloatingDecimal fd
3309                         = FormattedFloatingDecimal.valueOf(value, prec,
3310                           FormattedFloatingDecimal.Form.GENERAL);
3311                     exp = fd.getExponent();
3312                     mant = fd.getMantissa();
3313                     expRounded = fd.getExponentRounded();
3314                 }
3315 

3316                 if (exp != null) {
3317                     prec -= 1;
3318                 } else {
3319                     prec -= expRounded + 1;
3320                 }
3321 
3322                 mant = addZeros(mant, prec);
3323                 // If the precision is zero and the '#' flag is set, add the
3324                 // requested decimal point.
3325                 if (f.contains(Flags.ALTERNATE) && (prec == 0))
3326                     mant = addDot(mant);
3327 
3328                 int newW = width;
3329                 if (width != -1) {
3330                     if (exp != null)
3331                         newW = adjustWidth(width - exp.length - 1, f, neg);
3332                     else
3333                         newW = adjustWidth(width, f, neg);
3334                 }
3335                 localizedMagnitude(sb, mant, f, newW, l);
3336 
3337                 if (exp != null) {
3338                     sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');
3339 
3340                     Flags flags = f.dup().remove(Flags.GROUP);
3341                     char sign = exp[0];
3342                     assert(sign == '+' || sign == '-');