/** * Append a double in decimal notation to a specific number of decimal places. Trailing zeros are not truncated. * <p> * If the number would normally be printed with more decimal places, the number is rounded. * * @param d to append * @param decimalPlaces to always produce * @return this * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer */ @NotNull default B append(double d, int decimalPlaces) throws BufferOverflowException { if (decimalPlaces < 0) throw new IllegalArgumentException(); if (decimalPlaces < 20) { double d2 = d * Maths.tens(decimalPlaces); if (d2 <= Long.MAX_VALUE && d2 >= Long.MIN_VALUE) { // changed from java.lang.Math.round(d2) as this was shown up to cause latency long round = d2 > 0.0 ? (long) (d2 + 0.5) : (long) (d2 - 0.5); return appendDecimal(round, decimalPlaces); } } return append(d); }