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
ConstructorsConstructorDescriptionCreates a new formatter for numeric types with default settings. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringCreates the formatting string used byString.format(String, Object...)to display decimal values.protected StringCreates 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 NumberFormatterReturns the default number formatter.Returns the locale used when formatting the data.intReturns the precision of this field.getType()Returns the class (or classes using inheritance) this formatter supports.intgetWidth()Returns the width of this field.booleanReturnstrueif this format reserves a leading space for the sign.booleanReturnstrueif this formatting uses locale-specific groupings.booleanReturnstrueif this formatting uses scientific notation.static voidsetInstance(NumberFormatter instance) Sets the default number formatter.voidsetLeadingSpaceForSign(boolean leadingSpaceForSign) Set totrueto reserve a leading space for the sign;falseotherwise.voidSets the locale used when formatting the data.voidsetLocaleSpecificGroupings(boolean localeSpecificGroupings) Set totrueto use locale-specific groupings.voidsetPrecision(int precision) Sets the precision of this field.voidsetScientificNotation(boolean scientificNotation) Set totrueto use scientific notation.voidsetWidth(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
-
getInstance
Returns the default number formatter. Changes made to this formatter will be reflected everywhere the default formatter is used.- Returns:
- the default number formatter
-
setInstance
Sets the default number formatter.- Parameters:
instance- the formatter to use as the default.
-
getType
Description copied from interface:FormatterReturns 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()Returnstrueif this format reserves a leading space for the sign.- Returns:
trueif this format reserves a leading space for the sign;falseotherwise
-
setLeadingSpaceForSign
public void setLeadingSpaceForSign(boolean leadingSpaceForSign) Set totrueto reserve a leading space for the sign;falseotherwise. This is equivalent to using the"-"format option. SeeString.format(String, Object...)for details.- Parameters:
leadingSpaceForSign-trueto reserve a leading space for the sign;falseotherwise
-
isLocaleSpecificGroupings
public boolean isLocaleSpecificGroupings()Returnstrueif this formatting uses locale-specific groupings.- Returns:
trueif this formatting uses locale-specific groupings;falseotherwise
-
setLocaleSpecificGroupings
public void setLocaleSpecificGroupings(boolean localeSpecificGroupings) Set totrueto use locale-specific groupings. For example, this may include adding a comma or other character to split thousands-places.- Parameters:
localeSpecificGroupings-trueif this formatting uses locale-specific groupings;falseotherwise
-
isScientificNotation
public boolean isScientificNotation()Returnstrueif this formatting uses scientific notation.- Returns:
trueif this formatting uses scientific notation;falseotherwise
-
setScientificNotation
public void setScientificNotation(boolean scientificNotation) Set totrueto use scientific notation. When enabled, the number will include the exponent and is useful when expressing very small or large numbers.- Parameters:
scientificNotation-trueif this formatting uses scientific notation;falseotherwise
-
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:FormatterFormats 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
-