Class LocaleFormatter

java.lang.Object
com.helger.commons.locale.LocaleFormatter

@Immutable public final class LocaleFormatter extends Object
Helper class to safely print numbers in a formatted way.
Author:
Philip Helger
  • Method Details

    • getFormatted

      @Nonnull public static String getFormatted(double dValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All calls to Double.toString(double) that are displayed to the user should instead use this method.
      Parameters:
      dValue - The value to be formatted.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormatted

      @Nonnull public static String getFormatted(int nValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All calls to Integer.toString(int) that are displayed to the user should instead use this method.
      Parameters:
      nValue - The value to be formatted.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormatted

      @Nonnull public static String getFormatted(long nValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All calls to Long.toString(long) that are displayed to the user should instead use this method.
      Parameters:
      nValue - The value to be formatted.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormatted

      @Nonnull public static String getFormatted(@Nonnull BigInteger aValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All calls to BigInteger.toString() that are displayed to the user should instead use this method.
      Parameters:
      aValue - The value to be formatted. May not be null.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormatted

      @Nonnull public static String getFormatted(@Nonnull BigDecimal aValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All calls to BigDecimal.toString() that are displayed to the user should instead use this method. By default a maximum of 3 fraction digits are shown.
      Parameters:
      aValue - The value to be formatted. May not be null.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormatted

      @Nonnull public static String getFormatted(@Nonnull BigDecimal aValue, @Nonnegative int nFractionDigits, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale.
      Parameters:
      aValue - The value to be formatted. May not be null.
      nFractionDigits - The number of fractional digits to use. Must be ≥ 0.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormattedWithAllFractionDigits

      @Nonnull public static String getFormattedWithAllFractionDigits(@Nonnull BigDecimal aValue, @Nonnull Locale aDisplayLocale)
      Format the passed value according to the rules specified by the given locale. All fraction digits of the passed value are displayed.
      Parameters:
      aValue - The value to be formatted. May not be null.
      aDisplayLocale - The locale to be used. May not be null.
      Returns:
      The formatted string.
    • getFormattedPercent

      @Nonnull public static String getFormattedPercent(double dValue, @Nonnull Locale aDisplayLocale)
      Format the given value as percentage. The "%" sign is automatically appended according to the requested locale. The number of fractional digits depend on the locale.
      Parameters:
      dValue - The value to be used. E.g. "0.125" will result in something like "12.5%"
      aDisplayLocale - The locale to use.
      Returns:
      The non-null formatted string.
    • getFormattedPercent

      @Nonnull public static String getFormattedPercent(double dValue, @Nonnegative int nFractionDigits, @Nonnull Locale aDisplayLocale)
      Format the given value as percentage. The "%" sign is automatically appended according to the requested locale.
      Parameters:
      dValue - The value to be used. E.g. "0.125" will result in something like "12.5%"
      nFractionDigits - The number of fractional digits to use. Must be ≥ 0.
      aDisplayLocale - The locale to use.
      Returns:
      The non-null formatted string.