Class PercentFormatter

java.lang.Object
com.norconex.commons.lang.PercentFormatter
All Implemented Interfaces:
Serializable

public class PercentFormatter extends Object implements Serializable
Formats percentage as string. This class is thread-safe. Methods expecting a denominator will always return zero percent if said denominator is zero.
Since:
1.4.0
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new percent formatter with no decimal precision and using the default system locale.
    PercentFormatter(int decimalPrecision, Locale locale)
    Creates a new percent formatter for the given local and decimal precision.
  • Method Summary

    Modifier and Type
    Method
    Description
    format(double fraction)
    Formats a fraction as percentage string.
    format(double numerator, double denominator)
    Formats fraction values as percentage string.
    static String
    format(double numerator, double denominator, int decimalPrecision, Locale locale)
    Formats fraction values as percentage with the given local and decimal precision.
    static String
    format(double fraction, int decimalPrecision, Locale locale)
    Formats a fraction as percentage with the given local and decimal precision.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PercentFormatter

      public PercentFormatter()
      Creates a new percent formatter with no decimal precision and using the default system locale.
    • PercentFormatter

      public PercentFormatter(int decimalPrecision, Locale locale)
      Creates a new percent formatter for the given local and decimal precision.
      Parameters:
      decimalPrecision - maximum number of decimals to display
      locale - a locale
  • Method Details

    • format

      public String format(double numerator, double denominator)
      Formats fraction values as percentage string.
      Parameters:
      numerator - the value to divide
      denominator - the divider
      Returns:
      formatted percent string
    • format

      public String format(double fraction)
      Formats a fraction as percentage string.
      Parameters:
      fraction - the value to format as percentage
      Returns:
      formatted percent string
    • format

      public static String format(double numerator, double denominator, int decimalPrecision, Locale locale)
      Formats fraction values as percentage with the given local and decimal precision.
      Parameters:
      numerator - the value to divide
      denominator - the divider
      decimalPrecision - maximum number of decimals to display
      locale - a locale
      Returns:
      formatted percent string
    • format

      public static String format(double fraction, int decimalPrecision, Locale locale)
      Formats a fraction as percentage with the given local and decimal precision.
      Parameters:
      fraction - the value to format as percentage
      decimalPrecision - maximum number of decimals to display
      locale - a locale
      Returns:
      formatted percent string