< prev index next >

src/java.base/share/classes/java/math/BigInteger.java

Print this page

        

*** 3958,3967 **** --- 3958,3980 ---- toString(this, sb, radix, 0); return sb.toString(); } + private static void padWithZeros(StringBuilder buf, int len, int digits) { + if (digits > 0 && len < digits) { + int m = digits - len; + while (m >= NUM_ZEROS) { + buf.append(ZEROS); + m -= NUM_ZEROS; + } + if (m > 0) { + buf.append(ZEROS, 0, m); + } + } + } + /** * This method is used to perform toString when arguments are small. * A negative sign will be prepended if and only if {@code this < 0}. * If {@code digits <= 0} no padding (pre-pending with zeros) will be * effected.
*** 3971,3980 **** --- 3984,3994 ---- * @param digits The minimum number of digits to pad to. */ private void smallToString(int radix, StringBuilder buf, int digits) { if (signum == 0) { buf.append('0'); + padWithZeros(buf, 1, digits); return; } // Put sign (if any) into result buffer if (signum < 0) {
*** 4004,4037 **** // Get string version of first digit group String s = Long.toString(digitGroups[numGroups-1], radix); // Pad with internal zeros if necessary. ! if (digits > 0) { ! int len = s.length() + (numGroups - 1)*digitsPerLong[radix]; ! if (len < digits) { ! int m = digits - len; ! while (m >= NUM_ZEROS) { ! buf.append(zeros); ! m -= NUM_ZEROS; ! } ! if (m > 0) { ! buf.append(zeros, 0, m); ! } ! } ! } // Put first digit group into result buffer buf.append(s); // Append remaining digit groups each padded with leading zeros for (int i=numGroups-2; i >= 0; i--) { // Prepend (any) leading zeros for this digit group s = Long.toString(digitGroups[i], radix); int numLeadingZeros = digitsPerLong[radix] - s.length(); if (numLeadingZeros != 0) { ! buf.append(zeros, 0, numLeadingZeros); } buf.append(s); } } --- 4018,4040 ---- // Get string version of first digit group String s = Long.toString(digitGroups[numGroups-1], radix); // Pad with internal zeros if necessary. ! padWithZeros(buf, s.length() + (numGroups - 1)*digitsPerLong[radix], ! digits); // Put first digit group into result buffer buf.append(s); // Append remaining digit groups each padded with leading zeros for (int i=numGroups-2; i >= 0; i--) { // Prepend (any) leading zeros for this digit group s = Long.toString(digitGroups[i], radix); int numLeadingZeros = digitsPerLong[radix] - s.length(); if (numLeadingZeros != 0) { ! buf.append(ZEROS, 0, numLeadingZeros); } buf.append(s); } }
*** 4112,4126 **** powerCache = pc; // volatile write, publish } return cacheLine[exponent]; } ! /* Size of zeros string. */ private static int NUM_ZEROS = 63; ! /* zero is a string of NUM_ZEROS consecutive zeros. */ ! private static final String zeros = "0".repeat(NUM_ZEROS); /** * Returns the decimal String representation of this BigInteger. * The digit-to-character mapping provided by * {@code Character.forDigit} is used, and a minus sign is --- 4115,4129 ---- powerCache = pc; // volatile write, publish } return cacheLine[exponent]; } ! /* Size of ZEROS string. */ private static int NUM_ZEROS = 63; ! /* ZEROS is a string of NUM_ZEROS consecutive zeros. */ ! private static final String ZEROS = "0".repeat(NUM_ZEROS); /** * Returns the decimal String representation of this BigInteger. * The digit-to-character mapping provided by * {@code Character.forDigit} is used, and a minus sign is
< prev index next >