default double readStopBitDecimal() throws BufferOverflowException { long value = readStopBit(); int scale = (int) (Math.abs(value) % 10); value /= 10; return (double) value / Maths.tens(scale); }
@NotNull default R append(long offset, double value, int decimalPlaces, int digits) throws BufferOverflowException { if (decimalPlaces < 20) { double d2 = value * Maths.tens(decimalPlaces); if (d2 <= Long.MAX_VALUE && d2 >= Long.MIN_VALUE) { BytesInternal.appendDecimal(this, Math.round(d2), offset, decimalPlaces, digits); return (R) this; } } BytesInternal.append((StreamingDataOutput) this, value); return (R) this; }
/** * Append a float in decimal notation * * @param f to append * @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(float f) throws BufferOverflowException { float f2 = Math.abs(f); if (f2 > 1e6 || f2 < 1e-3) { return append(Float.toString(f)); } int precision = (int) Math.floor(6 - Math.log10(f2)); long tens = Maths.tens(precision); return append((double) Math.round(f * tens) / tens); }
/** * 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); }