Package com.helger.commons.locale
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetFormatted(double dValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormatted(int nValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormatted(long nValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormatted(BigDecimal aValue, int nFractionDigits, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormatted(BigDecimal aValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormatted(BigInteger aValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.static StringgetFormattedPercent(double dValue, int nFractionDigits, Locale aDisplayLocale)Format the given value as percentage.static StringgetFormattedPercent(double dValue, Locale aDisplayLocale)Format the given value as percentage.static StringgetFormattedWithAllFractionDigits(BigDecimal aValue, Locale aDisplayLocale)Format the passed value according to the rules specified by the given locale.
-
-
-
Method Detail
-
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 toDouble.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 benull.- 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 toInteger.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 benull.- 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 toLong.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 benull.- 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 toBigInteger.toString()that are displayed to the user should instead use this method.- Parameters:
aValue- The value to be formatted. May not benull.aDisplayLocale- The locale to be used. May not benull.- 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 toBigDecimal.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 benull.aDisplayLocale- The locale to be used. May not benull.- 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 benull.nFractionDigits- The number of fractional digits to use. Must be ≥ 0.aDisplayLocale- The locale to be used. May not benull.- 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 benull.aDisplayLocale- The locale to be used. May not benull.- 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-
nullformatted 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-
nullformatted string.
-
-