public class ScientificFormat extends NumberFormat
The aim of this class is to provide a flexible way to format large floating-point numbers. Its three modes allow one to format a fixed-precision decimal format, fixed-precision scientific notation, or a flexible format. With the flexible mode, scientific notation will be used if the decimal format would be larger than a specified number of characters (similar to the %g format in the C function printf).
The format can be fully configured just as its cousin, java.text.DecimalFormat, can. The pattern syntax supported by the applyPattern() method is a superset of that supported by DecimalFormat. (In fact, when a pure DecimalFormat pattern is used with this class, the resulting format is identical to that of DecimalFormat.) Below is a semi-formal description of the pattern syntax (see also java.text.DecimalFormat API documentation):
Patterns:
pattern := ('!'exppattern | '~altpattern' | {'@'}decipattern) decipattern := a DecimalFormat-supported pattern altpattern := decipattern{:int}{^intpattern} exppattern := decipattern{^intpattern} intpattern := subintpattern{;subintpattern} subintpattern := {prefix}integer{suffix} int := an integer number prefix,suffix := '\\u0000'..'\\uFFFD'One uses the !exppattern to force an exponential format,
@
decipattern for a decimal format, and ~altpattern for a
flexible format. The ^intpattern specificly controls the
format of the exponent when applicable. The default pattern is
"~#0.####;-#0.####:9^E+#0;E-#0
"; that is, it uses the
flexible mode that results in decimal format when the number can be
formatted to less than 9 characters and exponential otherwise. This author finds the pattern syntax sufficiently opaque that he recommends instantiating with the default behavior and then modifying it via the various set methods. For example, setMaxWidth() will change the cut-off width between decimal and exponential formats. The set...Mode() methods can be used to switch between the different formatting modes.
DecimalFormat
,
Serialized FormNumberFormat.Field
Modifier and Type | Field and Description |
---|---|
protected static String |
defdfmt |
protected static String |
defefmt |
protected DecimalFormat |
dfmt |
protected DecimalFormat |
efmt |
static int |
EXPONENT_FIELD |
protected int |
maxwidth |
FRACTION_FIELD, INTEGER_FIELD
Constructor and Description |
---|
ScientificFormat()
Create a ScientificFormat using the default pattern and symbols
for the default locale.
|
ScientificFormat(Locale locale)
Create a ScientificFormat using the default pattern for the
given locale.
|
ScientificFormat(Locale locale,
String pattern)
Creates a ScientificFormat using a given pattern for the specified
locale
|
ScientificFormat(String pattern)
Creates a ScientificFormat using a given pattern
|
format, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, hashCode, isParseIntegerOnly, parse, parseObject, setCurrency, setRoundingMode
format, formatToCharacterIterator, parseObject
protected DecimalFormat efmt
protected DecimalFormat dfmt
protected int maxwidth
protected static final String defdfmt
protected static final String defefmt
public static final int EXPONENT_FIELD
public ScientificFormat()
public ScientificFormat(Locale locale)
public ScientificFormat(String pattern)
public void applyPattern(String pattern) throws IllegalArgumentException
IllegalArgumentException
protected static String replaceQuotedStrings(Vector strings, Vector positions, String input, int start)
public void applyLocalizedPattern(String pattern)
public void applyMantissaPattern(String pattern)
public void applyExponentPattern(String pattern)
public void setMaxWidth(int width)
public int getMaxWidth()
public String getPositivePrefix()
public void setPositivePrefix(String newValue)
public String getNegativePrefix()
public void setNegativePrefix(String newValue)
public String getPositiveSuffix()
public void setPositiveSuffix(String newValue)
public String getNegativeSuffix()
public void setNegativeSuffix(String newValue)
public String getExpPositivePrefix()
public void setExpPositivePrefix(String newValue)
public String getExpNegativePrefix()
public void setExpNegativePrefix(String newValue)
public String getExpPositiveSuffix()
public void setExpPositiveSuffix(String newValue)
public String getExpNegativeSuffix()
public void setExpNegativeSuffix(String newValue)
public void setExponentialMode()
public boolean isExponentialMode()
public void setDecimalMode()
public boolean isDecimalMode()
public void setFlexibleMode()
public boolean isFlexibleMode()
public int getGroupingSize()
public void setGroupingSize(int newValue)
public int getExpGroupingSize()
public void setExpGroupingSize(int newValue)
public boolean isDecimalSeparatorAlwaysShown()
public void setDecimalSeparatorAlwaysShown(boolean newValue)
public boolean equals(Object obj)
equals
in class NumberFormat
public Object clone()
clone
in class NumberFormat
public String toPattern()
public StringBuffer format(double number, StringBuffer result, FieldPosition fieldPosition)
format
in class NumberFormat
public StringBuffer format(long number, StringBuffer result, FieldPosition fieldPosition)
format
in class NumberFormat
public Number parse(String text, ParsePosition parsePosition)
parse
in class NumberFormat
public Number parseAsExponential(String text, ParsePosition parsePosition)
public Number parseAsDecimal(String text, ParsePosition parsePosition)
public Number parseAsFlexible(String text, ParsePosition parsePosition)
public void setParseIntegerOnly(boolean value)
setParseIntegerOnly
in class NumberFormat
public boolean isGroupingUsed()
isGroupingUsed
in class NumberFormat
public void setGroupingUsed(boolean newValue)
setGroupingUsed
in class NumberFormat
public int getMaximumIntegerDigits()
getMaximumIntegerDigits
in class NumberFormat
public void setMaximumIntegerDigits(int newValue)
setMaximumIntegerDigits
in class NumberFormat
public int getMinimumIntegerDigits()
getMinimumIntegerDigits
in class NumberFormat
public void setMinimumIntegerDigits(int newValue)
setMinimumIntegerDigits
in class NumberFormat
public int getMaximumFractionDigits()
getMaximumFractionDigits
in class NumberFormat
public void setMaximumFractionDigits(int newValue)
setMaximumFractionDigits
in class NumberFormat
public int getMinimumFractionDigits()
getMinimumFractionDigits
in class NumberFormat
public void setMinimumFractionDigits(int newValue)
setMinimumFractionDigits
in class NumberFormat
public int getExpMaximumIntegerDigits()
public void setExpMaximumIntegerDigits(int newValue)
public int getExpMinimumIntegerDigits()
public void setExpMinimumIntegerDigits(int newValue)
public static void main(String[] args)
Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.