/** * Appends the textual representation of the specified <code>double</code> according to the * specified formatting arguments. * * @param d the <code>double</code> value. * @param digits the number of significative digits (excludes exponent) or <code>-1</code> to * mimic the standard library (16 or 17 digits). * @param scientific <code>true</code> to forces the use of the scientific notation (e.g. <code> * 1.23E3</code>); <code>false</code> otherwise. * @param showZero <code>true</code> if trailing fractional zeros are represented; <code>false * </code> otherwise. * @return <code>TypeFormat.format(d, digits, scientific, showZero, this)</code> * @throws IllegalArgumentException if <code>(digits > 19)</code>) */ public final TextBuilder append(double d, int digits, boolean scientific, boolean showZero) { if (digits > 19) throw new IllegalArgumentException("digits: " + digits); if (d != d) // NaN return append("NaN"); if (d == Double.POSITIVE_INFINITY) return append("Infinity"); if (d == Double.NEGATIVE_INFINITY) return append("-Infinity"); if (d == 0.0) { // Zero. if (digits < 0) return append("0.0"); append('0'); if (showZero) { append('.'); for (int j = 1; j < digits; j++) { append('0'); } } return this; } if (d < 0) { // Work with positive number. d = -d; append('-'); } // Find the exponent e such as: value == x.xxx * 10^e int e = MathLib.floorLog10(d); long m; if (digits < 0) { // Use 16 or 17 digits. // Try 17 digits. long m17 = MathLib.toLongPow10(d, (17 - 1) - e); // Check if we can use 16 digits. long m16 = m17 / 10; double dd = MathLib.toDoublePow10(m16, e - 16 + 1); if (dd == d) { // 16 digits is enough. digits = 16; m = m16; } else { // We cannot remove the last digit. digits = 17; m = m17; } } else // Use the specified number of digits. m = MathLib.toLongPow10(d, (digits - 1) - e); // Formats. if (scientific || (e >= digits)) { // Scientific notation has to be used ("x.xxxEyy"). long pow10 = POW10_LONG[digits - 1]; int k = (int) (m / pow10); // Single digit. append((char) ('0' + k)); m = m - pow10 * k; appendFraction(m, digits - 1, showZero); append('E'); append(e); } else { // Dot within the string ("xxxx.xxxxx"). int exp = digits - e - 1; if (exp < POW10_LONG.length) { long pow10 = POW10_LONG[exp]; long l = m / pow10; append(l); m = m - pow10 * l; } else append('0'); // Result of the division by a power of 10 larger than any long. appendFraction(m, exp, showZero); } return this; }