Package org.moeaframework.util.format
Class NumberFormatter
java.lang.Object
org.moeaframework.util.format.NumberFormatter
Formatter for displaying numeric data. This supports all primitive numeric types as well as their boxed types.
-
Constructor Summary
ConstructorDescriptionCreates a new formatter for numeric types with default settings. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
Creates the formatting string used byString.format(String, Object...)
to display decimal values.protected String
Creates the formatting string used byString.format(String, Object...)
to display integer values.format
(double[] values) Formats an array of double values into a string representation.format
(int[] values) Formats an array of integer values into a string representation.Formats the given value into a string representation.static NumberFormatter
Returns the default number formatter.Returns the locale used when formatting the data.int
Returns the precision of this field.getType()
Returns the class (or classes using inheritance) this formatter supports.int
getWidth()
Returns the width of this field.boolean
Returnstrue
if this format reserves a leading space for the sign.boolean
Returnstrue
if this formatting uses locale-specific groupings.boolean
Returnstrue
if this formatting uses scientific notation.static void
setDefault
(NumberFormatter formatter) Sets the default number formatter.void
setLeadingSpaceForSign
(boolean leadingSpaceForSign) Set totrue
to reserve a leading space for the sign;false
otherwise.void
Sets the locale used when formatting the data.void
setLocaleSpecificGroupings
(boolean localeSpecificGroupings) Set totrue
to use locale-specific groupings.void
setPrecision
(int precision) Sets the precision of this field.void
setScientificNotation
(boolean scientificNotation) Set totrue
to use scientific notation.void
setWidth
(int width) Sets the width of this field.
-
Constructor Details
-
NumberFormatter
public NumberFormatter()Creates a new formatter for numeric types with default settings.
-
-
Method Details
-
getDefault
Returns the default number formatter. Changes made to this formatter will be reflected everywhere the default formatter is used.- Returns:
- the default number formatter
-
setDefault
Sets the default number formatter.- Parameters:
formatter
- the formatter to use as the default; ifnull
, resets to the default settings.
-
getType
Description copied from interface:Formatter
Returns the class (or classes using inheritance) this formatter supports. -
getWidth
public int getWidth()Returns the width of this field.- Returns:
- the width
-
setWidth
public void setWidth(int width) Sets the width of this field. SeeString.format(String, Object...)
for details.- Parameters:
width
- the width
-
getPrecision
public int getPrecision()Returns the precision of this field.- Returns:
- the precision
-
setPrecision
public void setPrecision(int precision) Sets the precision of this field. SeeString.format(String, Object...)
for details.- Parameters:
precision
- the precision
-
isLeadingSpaceForSign
public boolean isLeadingSpaceForSign()Returnstrue
if this format reserves a leading space for the sign.- Returns:
true
if this format reserves a leading space for the sign;false
otherwise
-
setLeadingSpaceForSign
public void setLeadingSpaceForSign(boolean leadingSpaceForSign) Set totrue
to reserve a leading space for the sign;false
otherwise. This is equivalent to using the"-"
format option. SeeString.format(String, Object...)
for details.- Parameters:
leadingSpaceForSign
-true
to reserve a leading space for the sign;false
otherwise
-
isLocaleSpecificGroupings
public boolean isLocaleSpecificGroupings()Returnstrue
if this formatting uses locale-specific groupings.- Returns:
true
if this formatting uses locale-specific groupings;false
otherwise
-
setLocaleSpecificGroupings
public void setLocaleSpecificGroupings(boolean localeSpecificGroupings) Set totrue
to use locale-specific groupings. For example, this may include adding a comma or other character to split thousands-places.- Parameters:
localeSpecificGroupings
-true
if this formatting uses locale-specific groupings;false
otherwise
-
isScientificNotation
public boolean isScientificNotation()Returnstrue
if this formatting uses scientific notation.- Returns:
true
if this formatting uses scientific notation;false
otherwise
-
setScientificNotation
public void setScientificNotation(boolean scientificNotation) Set totrue
to use scientific notation. When enabled, the number will include the exponent and is useful when expressing very small or large numbers.- Parameters:
scientificNotation
-true
if this formatting uses scientific notation;false
otherwise
-
getLocale
Returns the locale used when formatting the data.- Returns:
- the locale used when formatting the data
-
setLocale
Sets the locale used when formatting the data. If unset, the default system locale is used.- Parameters:
locale
- the new locale
-
createDecimalFormatString
Creates the formatting string used byString.format(String, Object...)
to display decimal values.- Returns:
- the formatting string
-
createIntegerFormatString
Creates the formatting string used byString.format(String, Object...)
to display integer values.- Returns:
- the formatting string
-
format
Description copied from interface:Formatter
Formats the given value into a string representation. -
format
Formats an array of double values into a string representation.- Parameters:
values
- the values- Returns:
- the string representation
-
format
Formats an array of integer values into a string representation.- Parameters:
values
- the values- Returns:
- the string representation
-