Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Class ncsa.horizon.util.ScientificFormat

java.lang.Object
  |
  +--java.text.Format
        |
        +--java.text.NumberFormat
              |
              +--ncsa.horizon.util.ScientificFormat

public class ScientificFormat
extends java.text.NumberFormat
a formatter that uses a format suitable for numeric values in a scientific context.

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.

See Also:
java.text.DecimalFormat, Serialized Form

Field Summary
static java.lang.String defdfmt
           
static java.lang.String defefmt
           
java.text.DecimalFormat dfmt
           
java.text.DecimalFormat efmt
           
static int EXPONENT_FIELD
           
int maxwidth
           
 
Fields inherited from class java.text.NumberFormat
FRACTION_FIELD, INTEGER_FIELD
 
Constructor Summary
ScientificFormat()
          Create a ScientificFormat using the default pattern and symbols for the default locale.
ScientificFormat(java.util.Locale locale)
          Create a ScientificFormat using the default pattern for the given locale.
ScientificFormat(java.lang.String pattern)
          Creates a ScientificFormat using a given pattern
ScientificFormat(java.util.Locale locale, java.lang.String pattern)
          Creates a ScientificFormat using a given pattern for the specified locale
 
Method Summary
void applyExponentPattern(java.lang.String pattern)
           
void applyLocalizedPattern(java.lang.String pattern)
           
void applyMantissaPattern(java.lang.String pattern)
           
void applyPattern(java.lang.String pattern)
          apply this pattern
java.lang.Object clone()
           
boolean equals(java.lang.Object obj)
           
static java.lang.Object[] extractQuotedStrings(java.lang.String text)
           
java.lang.StringBuffer format(double number, java.lang.StringBuffer result, java.text.FieldPosition fieldPosition)
           
java.lang.StringBuffer format(long number, java.lang.StringBuffer result, java.text.FieldPosition fieldPosition)
           
int getExpGroupingSize()
           
int getExpMaximumIntegerDigits()
           
int getExpMinimumIntegerDigits()
           
java.lang.String getExpNegativePrefix()
           
java.lang.String getExpNegativeSuffix()
           
java.lang.String getExpPositivePrefix()
           
java.lang.String getExpPositiveSuffix()
           
int getGroupingSize()
           
int getMaximumFractionDigits()
           
int getMaximumIntegerDigits()
           
int getMaxWidth()
           
int getMinimumFractionDigits()
           
int getMinimumIntegerDigits()
           
java.lang.String getNegativePrefix()
           
java.lang.String getNegativeSuffix()
           
java.lang.String getPositivePrefix()
           
java.lang.String getPositiveSuffix()
           
boolean isDecimalMode()
           
boolean isDecimalSeparatorAlwaysShown()
           
boolean isExponentialMode()
           
boolean isFlexibleMode()
           
boolean isGroupingUsed()
           
static void main(java.lang.String[] args)
           
java.lang.Number parseAsDecimal(java.lang.String text, java.text.ParsePosition parsePosition)
          parse the input string, interpreting it as regular decimal format.
java.lang.Number parseAsExponential(java.lang.String text, java.text.ParsePosition parsePosition)
          parse the input string, interpreting it as exponential format.
java.lang.Number parseAsFlexible(java.lang.String text, java.text.ParsePosition parsePosition)
          parse the input string, interpreting it as either regular decimal format or exponential format.
java.lang.Number parse(java.lang.String text, java.text.ParsePosition parsePosition)
          Parse out a number from the given text and return it as a Long if possible; otherwise, return it as a Double.
static java.lang.String replaceQuotedStrings(java.util.Vector strings, java.util.Vector positions, java.lang.String input, int start)
           
void setDecimalMode()
           
void setDecimalSeparatorAlwaysShown(boolean newValue)
           
void setExpGroupingSize(int newValue)
           
void setExpMaximumIntegerDigits(int newValue)
           
void setExpMinimumIntegerDigits(int newValue)
           
void setExpNegativePrefix(java.lang.String newValue)
           
void setExpNegativeSuffix(java.lang.String newValue)
           
void setExponentialMode()
           
void setExpPositivePrefix(java.lang.String newValue)
           
void setExpPositiveSuffix(java.lang.String newValue)
           
void setFlexibleMode()
           
void setGroupingSize(int newValue)
           
void setGroupingUsed(boolean newValue)
           
void setMaximumFractionDigits(int newValue)
           
void setMaximumIntegerDigits(int newValue)
           
void setMaxWidth(int width)
          the the maximum width of the decimal format.
void setMinimumFractionDigits(int newValue)
           
void setMinimumIntegerDigits(int newValue)
           
void setNegativePrefix(java.lang.String newValue)
           
void setNegativeSuffix(java.lang.String newValue)
           
void setParseIntegerOnly(boolean value)
           
void setPositivePrefix(java.lang.String newValue)
           
void setPositiveSuffix(java.lang.String newValue)
           
java.lang.String toPattern()
           
 
Methods inherited from class java.text.NumberFormat
clone, equals, format, format, format, format, format, getAvailableLocales, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, hashCode, isGroupingUsed, isParseIntegerOnly, parse, parse, parseObject, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly
 
Methods inherited from class java.text.Format
clone, format, format, parseObject, parseObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait
 

Field Detail

efmt

protected java.text.DecimalFormat efmt

dfmt

protected java.text.DecimalFormat dfmt

maxwidth

protected int maxwidth

defdfmt

protected static final java.lang.String defdfmt

defefmt

protected static final java.lang.String defefmt

EXPONENT_FIELD

public static final int EXPONENT_FIELD
Constructor Detail

ScientificFormat

public ScientificFormat()
Create a ScientificFormat using the default pattern and symbols for the default locale.

ScientificFormat

public ScientificFormat(java.util.Locale locale)
Create a ScientificFormat using the default pattern for the given locale.

ScientificFormat

public ScientificFormat(java.lang.String pattern)
Creates a ScientificFormat using a given pattern

ScientificFormat

public ScientificFormat(java.util.Locale locale,
                        java.lang.String pattern)
Creates a ScientificFormat using a given pattern for the specified locale
Method Detail

applyPattern

public void applyPattern(java.lang.String pattern)
                 throws java.lang.IllegalArgumentException
apply this pattern

extractQuotedStrings

protected static java.lang.Object[] extractQuotedStrings(java.lang.String text)

replaceQuotedStrings

protected static java.lang.String replaceQuotedStrings(java.util.Vector strings,
                                             java.util.Vector positions,
                                             java.lang.String input,
                                             int start)

applyLocalizedPattern

public void applyLocalizedPattern(java.lang.String pattern)

applyMantissaPattern

public void applyMantissaPattern(java.lang.String pattern)

applyExponentPattern

public void applyExponentPattern(java.lang.String pattern)

setMaxWidth

public void setMaxWidth(int width)
the the maximum width of the decimal format. This is used only while in flexible mode; if the decimal is greater than this width, exponential mode will be used. A negative value means use the default width.

getMaxWidth

public int getMaxWidth()

getPositivePrefix

public java.lang.String getPositivePrefix()

setPositivePrefix

public void setPositivePrefix(java.lang.String newValue)

getNegativePrefix

public java.lang.String getNegativePrefix()

setNegativePrefix

public void setNegativePrefix(java.lang.String newValue)

getPositiveSuffix

public java.lang.String getPositiveSuffix()

setPositiveSuffix

public void setPositiveSuffix(java.lang.String newValue)

getNegativeSuffix

public java.lang.String getNegativeSuffix()

setNegativeSuffix

public void setNegativeSuffix(java.lang.String newValue)

getExpPositivePrefix

public java.lang.String getExpPositivePrefix()

setExpPositivePrefix

public void setExpPositivePrefix(java.lang.String newValue)

getExpNegativePrefix

public java.lang.String getExpNegativePrefix()

setExpNegativePrefix

public void setExpNegativePrefix(java.lang.String newValue)

getExpPositiveSuffix

public java.lang.String getExpPositiveSuffix()

setExpPositiveSuffix

public void setExpPositiveSuffix(java.lang.String newValue)

getExpNegativeSuffix

public java.lang.String getExpNegativeSuffix()

setExpNegativeSuffix

public void setExpNegativeSuffix(java.lang.String newValue)

setExponentialMode

public void setExponentialMode()

isExponentialMode

public boolean isExponentialMode()

setDecimalMode

public void setDecimalMode()

isDecimalMode

public boolean isDecimalMode()

setFlexibleMode

public void setFlexibleMode()

isFlexibleMode

public boolean isFlexibleMode()

getGroupingSize

public int getGroupingSize()

setGroupingSize

public void setGroupingSize(int newValue)

getExpGroupingSize

public int getExpGroupingSize()

setExpGroupingSize

public void setExpGroupingSize(int newValue)

isDecimalSeparatorAlwaysShown

public boolean isDecimalSeparatorAlwaysShown()

setDecimalSeparatorAlwaysShown

public void setDecimalSeparatorAlwaysShown(boolean newValue)

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.text.NumberFormat

clone

public java.lang.Object clone()
Overrides:
clone in class java.text.NumberFormat

toPattern

public java.lang.String toPattern()

format

public java.lang.StringBuffer format(double number,
                           java.lang.StringBuffer result,
                           java.text.FieldPosition fieldPosition)
Overrides:
format in class java.text.NumberFormat

format

public java.lang.StringBuffer format(long number,
                           java.lang.StringBuffer result,
                           java.text.FieldPosition fieldPosition)
Overrides:
format in class java.text.NumberFormat

parse

public java.lang.Number parse(java.lang.String text,
                    java.text.ParsePosition parsePosition)
Parse out a number from the given text and return it as a Long if possible; otherwise, return it as a Double. If parseIntegerOnly is set, parsing will stop at a decimal point (or equivalent). How the string is interpreted (i.e. as a normal decimal or as exponential notation) depends on the currently set mode.
Overrides:
parse in class java.text.NumberFormat

parseAsExponential

public java.lang.Number parseAsExponential(java.lang.String text,
                                 java.text.ParsePosition parsePosition)
parse the input string, interpreting it as exponential format. If parseIntegerOnly is set, return only the integer portion of the mantissa. (To return the decimal portion of the mantissa, use parseAsDecimal()).

parseAsDecimal

public java.lang.Number parseAsDecimal(java.lang.String text,
                             java.text.ParsePosition parsePosition)
parse the input string, interpreting it as regular decimal format. This behaves just as DecimalFormat.parse().

parseAsFlexible

public java.lang.Number parseAsFlexible(java.lang.String text,
                              java.text.ParsePosition parsePosition)
parse the input string, interpreting it as either regular decimal format or exponential format. Exponential format is tried first; failing that decimal format is tried.

setParseIntegerOnly

public void setParseIntegerOnly(boolean value)
Overrides:
setParseIntegerOnly in class java.text.NumberFormat

isGroupingUsed

public boolean isGroupingUsed()
Overrides:
isGroupingUsed in class java.text.NumberFormat

setGroupingUsed

public void setGroupingUsed(boolean newValue)
Overrides:
setGroupingUsed in class java.text.NumberFormat

getMaximumIntegerDigits

public int getMaximumIntegerDigits()
Overrides:
getMaximumIntegerDigits in class java.text.NumberFormat

setMaximumIntegerDigits

public void setMaximumIntegerDigits(int newValue)
Overrides:
setMaximumIntegerDigits in class java.text.NumberFormat

getMinimumIntegerDigits

public int getMinimumIntegerDigits()
Overrides:
getMinimumIntegerDigits in class java.text.NumberFormat

setMinimumIntegerDigits

public void setMinimumIntegerDigits(int newValue)
Overrides:
setMinimumIntegerDigits in class java.text.NumberFormat

getMaximumFractionDigits

public int getMaximumFractionDigits()
Overrides:
getMaximumFractionDigits in class java.text.NumberFormat

setMaximumFractionDigits

public void setMaximumFractionDigits(int newValue)
Overrides:
setMaximumFractionDigits in class java.text.NumberFormat

getMinimumFractionDigits

public int getMinimumFractionDigits()
Overrides:
getMinimumFractionDigits in class java.text.NumberFormat

setMinimumFractionDigits

public void setMinimumFractionDigits(int newValue)
Overrides:
setMinimumFractionDigits in class java.text.NumberFormat

getExpMaximumIntegerDigits

public int getExpMaximumIntegerDigits()

setExpMaximumIntegerDigits

public void setExpMaximumIntegerDigits(int newValue)

getExpMinimumIntegerDigits

public int getExpMinimumIntegerDigits()

setExpMinimumIntegerDigits

public void setExpMinimumIntegerDigits(int newValue)

main

public static void main(java.lang.String[] args)

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD