public final class NumberFormatter extends Object
This exists as an alternative to NumberFormat and DecimalFormat
which are not thread-safe.
Instances of this class are immutable and thread-safe.
| Modifier and Type | Method and Description |
|---|---|
String |
format(double number)
Formats a
double using this formatter. |
String |
format(long number)
Formats a
long using this formatter. |
static NumberFormatter |
of(boolean grouped,
int decimalPlaces)
Obtains a standard formatter configured by grouping and decimal places.
|
static NumberFormatter |
of(boolean grouped,
int minDecimalPlaces,
int maxDecimalPlaces)
Obtains a standard formatter configured by grouping and decimal places.
|
static NumberFormatter |
ofLocalizedNumber(Locale locale)
Obtains a number formatter for general-purpose use in the specified locale.
|
static NumberFormatter |
ofPattern(String pattern,
Locale locale)
Obtains a formatter based on a pattern in the specified locale.
|
static NumberFormatter |
ofPercentage(boolean grouped,
int minDecimalPlaces,
int maxDecimalPlaces)
Obtains a formatter for decimal percentages configured by grouping and decimal places.
|
double |
parse(String text)
Parses the specific string, returning a double.
|
String |
toString()
Returns a string representation of this formatter.
|
public static NumberFormatter of(boolean grouped, int decimalPlaces)
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
grouped - true to group, false to not groupdecimalPlaces - the minimum number of decimal places, from 0 to 9IllegalArgumentException - if the decimal places is invalidpublic static NumberFormatter of(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)
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
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 9IllegalArgumentException - if the decimal places is invalidpublic static NumberFormatter ofPercentage(boolean grouped, int minDecimalPlaces, int maxDecimalPlaces)
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_EVEN
The number passed in must be the decimal representation of the percentage. It will be multiplied by 100 before formatting.
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 9IllegalArgumentException - if the decimal places is invalidpublic static NumberFormatter ofPattern(String pattern, Locale locale)
pattern - the pattern string to uselocale - the locale to useIllegalArgumentException - if the pattern is invalidDecimalFormatpublic static NumberFormatter ofLocalizedNumber(Locale locale)
locale - the locale to useNumberFormat.getNumberInstance(Locale)public String format(double number)
double using this formatter.number - the number to formatpublic String format(long number)
long using this formatter.number - the number to formatpublic double parse(String text)
text - the string to parseIllegalArgumentException - if the text cannot be parsedCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.