/** * 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(); }
/** * 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(); }
/** * Returns a value which wraps {@code value + separator + this} * <p> * If the current value is empty, the given value is returned (without the separator). If the given * value is an empty string, the current value is returned (without the separator). * * @param separator the separator to be put in between the two. If the given value is <tt>null</tt>, "" is assumed * @param value the value to be appended to the current value. * @return a <tt>Value</tt> representing the given value appended with the current value and separated * with the given separator */ @Nonnull public Value prepend(@Nullable String separator, @Nullable Object value) { if (Strings.isEmpty(value)) { return this; } if (isEmptyString()) { return Value.of(value); } if (separator == null) { separator = ""; } return Value.of(value + separator + toString()); }
/** * Returns a value which wraps {@code this + separator + value} * <p> * If the current value is empty, the given value is returned (without the separator). If the given * value is an empty string, the current value is returned (without the separator). * * @param separator the separator to be put in between the two. If the given value is <tt>null</tt>, "" is assumed * @param value the value to be appended to the current value. * @return a <tt>Value</tt> representing the current value appended with the given value and separated * with the given separator */ @Nonnull public Value append(@Nullable String separator, @Nullable Object value) { if (Strings.isEmpty(value)) { return this; } if (isEmptyString()) { return Value.of(value); } if (separator == null) { separator = ""; } return Value.of(toString() + separator + value); }