/** * Formats the represented value by rounding to zero decimal places. The rounding mode is obtained from * {@link NumberFormat#NO_DECIMAL_PLACES}. * * @return a rounded representation of this number using {@code NumberFormat#NO_DECIMAL_PLACES} * or "" is the value is empty. */ public String toRoundedString() { return toSmartRoundedString(NumberFormat.NO_DECIMAL_PLACES).toString(); }
@Override public String toString() { return toSmartRoundedString(NumberFormat.TWO_DECIMAL_PLACES).toString(); }
/** * Formats the represented value as percentage. Up to two digits will be shown if non zero. * Therefore <tt>12.34</tt> will be rendered as <tt>12.34 %</tt> but <tt>5.00</tt> will be * rendered as <tt>5 %</tt>. If the value is empty, "" will be returned. * <p> * A custom {@link NumberFormat} can be used by directly calling {@link #toSmartRoundedString(NumberFormat)} * or to disable smart rounding (to also show .00) {@link #toString(NumberFormat)} can be called using * {@link NumberFormat#PERCENT}. * * @return a string representation of this number using {@code NumberFormat#PERCENT} * or "" if the value is empty. */ public String toPercentString() { return toSmartRoundedString(NumberFormat.PERCENT).toString(); }
/** * Converts a file or byte size. * <p> * Supports sizes up to petabyte. Uses conventional SI-prefixed abbreviations like kB, MB. * * @param size the size to format in bytes * @return an english representation (using dot as decimal separator) along with one of the known abbreviations: * <tt>Bytes, KB, MB, GB, TB, PB</tt>. */ public static String formatSize(long size) { int index = 0; double sizeAsFloat = size; while (sizeAsFloat > 1000 && index < UNITS.length - 1) { sizeAsFloat = sizeAsFloat / 1000; index++; } return Amount.of(sizeAsFloat).toSmartRoundedString(NumberFormat.MACHINE_TWO_DECIMAL_PLACES) + " " + UNITS[index]; } }
/** * String representation of the value along with its unit (is necessary) * * @return a string representation of the value */ public String getValueAsString() { return Amount.of(value).toSmartRoundedString(NumberFormat.TWO_DECIMAL_PLACES).append(" ", unit).toString(); }