Package org.apache.commons.text.numbers
Class DoubleFormat.Builder
java.lang.Object
org.apache.commons.text.numbers.DoubleFormat.Builder
- Enclosing class:
- DoubleFormat
Builds configured format functions for standard double format types.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Flag indicating if the exponent value should always be included, even if zero.private char
Decimal separator character.private static final String
Default decimal digit characters.private static final int
Default value for the plain format max decimal exponent.private static final int
Default value for the plain format min decimal exponent.private String
String of digit characters 0-9.private String
Exponent separator character.private final Function<DoubleFormat.Builder,
DoubleFunction<String>> Function used to construct format instances.private boolean
Flag determining if fraction placeholders should be used.private char
Character used to separate groups of thousands.private boolean
Iftrue
, thousands groups will be separated by the grouping separator.private String
String representing infinity.private int
Maximum number of significant decimal digits in formatted strings.private int
Minimum decimal exponent.private char
Minus sign character.private String
String representing NaN.private int
Max decimal exponent to use with plain formatting with the mixed format type.private int
Min decimal exponent to use with plain formatting with the mixed format type.private boolean
Flag determining if signed zero strings are allowed. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Builder
(Function<DoubleFormat.Builder, DoubleFunction<String>> factory) Builds a new instance that delegates double function construction to the given factory object. -
Method Summary
Modifier and TypeMethodDescriptionallowSignedZero
(boolean signedZero) Sets the flag determining whether or not the zero string may be returned with the minus sign or if it will always be returned in the positive form.alwaysIncludeExponent
(boolean alwaysIncludeExponent) Sets the flag indicating if an exponent value should always be included in the formatted value, even if the exponent value is zero.build()
Builds a new double format function.decimalSeparator
(char decimalSeparator) Sets the decimal separator character, i.e., the character placed between the whole number and fractional portions of the formatted strings.Sets the string containing the digit characters 0-9, in that order.exponentSeparator
(String exponentSeparator) Sets the exponent separator character, i.e., the string placed between the mantissa and the exponent.formatSymbols
(DecimalFormatSymbols symbols) Configures this instance with the given format symbols.private String
getDigitString
(DecimalFormatSymbols symbols) Gets a string containing the localized digits 0-9 for the given symbols object.groupingSeparator
(char groupingSeparator) Sets the character used to separate groups of thousands.groupThousands
(boolean groupThousands) If set totrue
, thousands will be grouped with thegrouping separator
.includeFractionPlaceholder
(boolean fractionPlaceholder) Sets the flag determining whether or not a zero character is added in the fraction position when no fractional value is present.Sets the string used to represent infinity.maxPrecision
(int maxPrecision) Sets the maximum number of significant decimal digits used in format results.minDecimalExponent
(int minDecimalExponent) Sets the minimum decimal exponent for formatted strings.minusSign
(char minusSign) Sets the character used as the minus sign.Sets the string used to representDouble.NaN
.plainFormatMaxDecimalExponent
(int plainFormatMaxDecimalExponent) Sets the maximum decimal exponent for numbers formatted as plain decimal strings when using theMIXED
format type.plainFormatMinDecimalExponent
(int plainFormatMinDecimalExponent) Sets the minimum decimal exponent for numbers formatted as plain decimal strings when using theMIXED
format type.
-
Field Details
-
DEFAULT_PLAIN_FORMAT_MAX_DECIMAL_EXPONENT
private static final int DEFAULT_PLAIN_FORMAT_MAX_DECIMAL_EXPONENTDefault value for the plain format max decimal exponent.- See Also:
-
DEFAULT_PLAIN_FORMAT_MIN_DECIMAL_EXPONENT
private static final int DEFAULT_PLAIN_FORMAT_MIN_DECIMAL_EXPONENTDefault value for the plain format min decimal exponent.- See Also:
-
DEFAULT_DECIMAL_DIGITS
Default decimal digit characters.- See Also:
-
factory
Function used to construct format instances. -
maxPrecision
private int maxPrecisionMaximum number of significant decimal digits in formatted strings. -
minDecimalExponent
private int minDecimalExponentMinimum decimal exponent. -
plainFormatMaxDecimalExponent
private int plainFormatMaxDecimalExponentMax decimal exponent to use with plain formatting with the mixed format type. -
plainFormatMinDecimalExponent
private int plainFormatMinDecimalExponentMin decimal exponent to use with plain formatting with the mixed format type. -
infinity
String representing infinity. -
nan
String representing NaN. -
fractionPlaceholder
private boolean fractionPlaceholderFlag determining if fraction placeholders should be used. -
signedZero
private boolean signedZeroFlag determining if signed zero strings are allowed. -
digits
String of digit characters 0-9. -
decimalSeparator
private char decimalSeparatorDecimal separator character. -
groupingSeparator
private char groupingSeparatorCharacter used to separate groups of thousands. -
groupThousands
private boolean groupThousandsIftrue
, thousands groups will be separated by the grouping separator. -
minusSign
private char minusSignMinus sign character. -
exponentSeparator
Exponent separator character. -
alwaysIncludeExponent
private boolean alwaysIncludeExponentFlag indicating if the exponent value should always be included, even if zero.
-
-
Constructor Details
-
Builder
Builds a new instance that delegates double function construction to the given factory object.- Parameters:
factory
- factory function
-
-
Method Details
-
allowSignedZero
Sets the flag determining whether or not the zero string may be returned with the minus sign or if it will always be returned in the positive form. For example, if set totrue
, the string"-0.0"
may be returned for some input numbers. Iffalse
, only"0.0"
will be returned, regardless of the sign of the input number. The default value istrue
.- Parameters:
signedZero
- iftrue
, the zero string may be returned with a preceding minus sign; iffalse
, the zero string will only be returned in its positive form- Returns:
- this instance
-
alwaysIncludeExponent
Sets the flag indicating if an exponent value should always be included in the formatted value, even if the exponent value is zero. This property only applies to formats that use scientific notation, namelySCIENTIFIC
,ENGINEERING
, andMIXED
. The default value isfalse
.- Parameters:
alwaysIncludeExponent
- iftrue
, exponents will always be included in formatted output even if the exponent value is zero- Returns:
- this instance
-
build
Builds a new double format function.- Returns:
- format function
-
decimalSeparator
Sets the decimal separator character, i.e., the character placed between the whole number and fractional portions of the formatted strings. The default value is'.'
.- Parameters:
decimalSeparator
- decimal separator character- Returns:
- this instance
-
digits
Sets the string containing the digit characters 0-9, in that order. The default value is the string"0123456789"
.- Parameters:
digits
- string containing the digit characters 0-9- Returns:
- this instance
- Throws:
NullPointerException
- if the argument isnull
IllegalArgumentException
- if the argument does not have a length of exactly 10
-
exponentSeparator
Sets the exponent separator character, i.e., the string placed between the mantissa and the exponent. The default value is"E"
, as in"1.2E6"
.- Parameters:
exponentSeparator
- exponent separator string- Returns:
- this instance
- Throws:
NullPointerException
- if the argument isnull
-
formatSymbols
Configures this instance with the given format symbols. The following values are set:digit characters
decimal separator
thousands grouping separator
minus sign
exponent separator
infinity
NaN
zero digit
and adding the next 9 consecutive characters.- Parameters:
symbols
- format symbols- Returns:
- this instance
- Throws:
NullPointerException
- if the argument isnull
-
getDigitString
Gets a string containing the localized digits 0-9 for the given symbols object. The string is constructed by starting at thezero digit
and adding the next 9 consecutive characters.- Parameters:
symbols
- symbols object- Returns:
- string containing the localized digits 0-9
-
groupingSeparator
Sets the character used to separate groups of thousands. Default value is','
.- Parameters:
groupingSeparator
- character used to separate groups of thousands- Returns:
- this instance
- See Also:
-
groupThousands
If set totrue
, thousands will be grouped with thegrouping separator
. For example, if set totrue
, the number1000
could be formatted as"1,000"
. This property only applies to thePLAIN
format. Default value isfalse
.- Parameters:
groupThousands
- iftrue
, thousands will be grouped- Returns:
- this instance
- See Also:
-
includeFractionPlaceholder
Sets the flag determining whether or not a zero character is added in the fraction position when no fractional value is present. For example, if set totrue
, the number1
would be formatted as"1.0"
. Iffalse
, it would be formatted as"1"
. The default value istrue
.- Parameters:
fractionPlaceholder
- iftrue
, a zero character is placed in the fraction position when no fractional value is present; iffalse
, fractional digits are only included when needed- Returns:
- this instance
-
infinity
Sets the string used to represent infinity. For negative infinity, this string is prefixed with theminus sign
.- Parameters:
infinity
- string used to represent infinity- Returns:
- this instance
- Throws:
NullPointerException
- if the argument isnull
-
maxPrecision
Sets the maximum number of significant decimal digits used in format results. A value of0
indicates no limit. The default value is0
.- Parameters:
maxPrecision
- maximum precision- Returns:
- this instance
-
minDecimalExponent
Sets the minimum decimal exponent for formatted strings. No digits with an absolute value of less than10minDecimalExponent
will be included in format results. If the number being formatted does not contain any such digits, then zero is returned. For example, ifminDecimalExponent
is set to-2
and the number3.14159
is formatted, the plain format result will be"3.14"
. If0.001
is formatted, then the result is the zero string.- Parameters:
minDecimalExponent
- minimum decimal exponent- Returns:
- this instance
-
minusSign
Sets the character used as the minus sign.- Parameters:
minusSign
- character to use as the minus sign- Returns:
- this instance
-
nan
Sets the string used to representDouble.NaN
.- Parameters:
nan
- string used to representDouble.NaN
- Returns:
- this instance
- Throws:
NullPointerException
- if the argument isnull
-
plainFormatMaxDecimalExponent
Sets the maximum decimal exponent for numbers formatted as plain decimal strings when using theMIXED
format type. If the number being formatted has an absolute value less than10plainFormatMaxDecimalExponent + 1
and greater than or equal to10plainFormatMinDecimalExponent
after any necessary rounding, then the formatted result will use thePLAIN
format type. Otherwise,SCIENTIFIC
format will be used. For example, if this value is set to2
, the number999
will be formatted as"999.0"
while1000
will be formatted as"1.0E3"
.The default value is 6.
This value is ignored for formats other than
DoubleFormat.MIXED
.- Parameters:
plainFormatMaxDecimalExponent
- maximum decimal exponent for values formatted as plain strings when using theMIXED
format type.- Returns:
- this instance
- See Also:
-
plainFormatMinDecimalExponent
Sets the minimum decimal exponent for numbers formatted as plain decimal strings when using theMIXED
format type. If the number being formatted has an absolute value less than10plainFormatMaxDecimalExponent + 1
and greater than or equal to10plainFormatMinDecimalExponent
after any necessary rounding, then the formatted result will use thePLAIN
format type. Otherwise,SCIENTIFIC
format will be used. For example, if this value is set to-2
, the number0.01
will be formatted as"0.01"
while0.0099
will be formatted as"9.9E-3"
.The default value is -3.
This value is ignored for formats other than
DoubleFormat.MIXED
.- Parameters:
plainFormatMinDecimalExponent
- maximum decimal exponent for values formatted as plain strings when using theMIXED
format type.- Returns:
- this instance
- See Also:
-