Class NumberFormatter
- java.lang.Object
-
- com.opengamma.strata.collect.NumberFormatter
-
public final class NumberFormatter extends Object
Provides the ability to parse and format numbers.This exists as an alternative to
NumberFormatandDecimalFormatwhich are not thread-safe.Instances of this class are immutable and thread-safe.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringformat(double number)Formats adoubleusing this formatter.Stringformat(long number)Formats alongusing this formatter.static NumberFormatterof(boolean grouped, int decimalPlaces)Obtains a standard formatter configured by grouping and decimal places.static NumberFormatterof(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)Obtains a standard formatter configured by grouping and decimal places.static NumberFormatterofLocalizedNumber(Locale locale)Obtains a number formatter for general-purpose use in the specified locale.static NumberFormatterofPattern(String pattern, Locale locale)Obtains a formatter based on a pattern in the specified locale.static NumberFormatterofPercentage(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)Obtains a formatter for decimal percentages configured by grouping and decimal places.doubleparse(String text)Parses the specific string, returning a double.StringtoString()Returns a string representation of this formatter.
-
-
-
Method Detail
-
of
public static NumberFormatter of(boolean grouped, int decimalPlaces)
Obtains a standard formatter configured by grouping and decimal places.The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). Numbers will be rounded using
RoundingMode.HALF_EVEN- Parameters:
grouped- true to group, false to not groupdecimalPlaces- the minimum number of decimal places, from 0 to 9- Returns:
- the formatter
- Throws:
IllegalArgumentException- if the decimal places is invalid
-
of
public static NumberFormatter of(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)
Obtains a standard formatter configured by grouping and decimal places.The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). Numbers will be rounded using
RoundingMode.HALF_EVEN- Parameters:
grouped- true to group, false to not groupminDecimalPlaces- the minimum number of decimal places, from 0 to 9maxDecimalPlaces- the minimum number of decimal places, from 0 to 9- Returns:
- the formatter
- Throws:
IllegalArgumentException- if the decimal places is invalid
-
ofPercentage
public static NumberFormatter ofPercentage(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)
Obtains a formatter for decimal percentages configured by grouping and decimal places.The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). The formatter will suffix the output with '%'. Numbers will be rounded using
RoundingMode.HALF_EVENThe number passed in must be the decimal representation of the percentage. It will be multiplied by 100 before formatting.
- Parameters:
grouped- true to group, false to not groupminDecimalPlaces- the minimum number of decimal places, from 0 to 9maxDecimalPlaces- the minimum number of decimal places, from 0 to 9- Returns:
- the formatter
- Throws:
IllegalArgumentException- if the decimal places is invalid
-
ofPattern
public static NumberFormatter ofPattern(String pattern, Locale locale)
Obtains a formatter based on a pattern in the specified locale.- Parameters:
pattern- the pattern string to uselocale- the locale to use- Returns:
- the formatter
- Throws:
IllegalArgumentException- if the pattern is invalid- See Also:
DecimalFormat
-
ofLocalizedNumber
public static NumberFormatter ofLocalizedNumber(Locale locale)
Obtains a number formatter for general-purpose use in the specified locale.- Parameters:
locale- the locale to use- Returns:
- the formatter
- See Also:
NumberFormat.getNumberInstance(Locale)
-
format
public String format(double number)
Formats adoubleusing this formatter.- Parameters:
number- the number to format- Returns:
- the formatted string
-
format
public String format(long number)
Formats alongusing this formatter.- Parameters:
number- the number to format- Returns:
- the formatted string
-
parse
public double parse(String text)
Parses the specific string, returning a double.- Parameters:
text- the string to parse- Returns:
- the parsed number
- Throws:
IllegalArgumentException- if the text cannot be parsed
-
-