uk.ac.starlink.ast.gui
Class ScientificFormat

java.lang.Object
  extended by java.text.Format
      extended by java.text.NumberFormat
          extended by uk.ac.starlink.ast.gui.ScientificFormat
All Implemented Interfaces:
Serializable, Cloneable

public class ScientificFormat
extends 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.

Version:
1.1
Author:
Raymond L. Plante
See Also:
DecimalFormat, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.NumberFormat
NumberFormat.Field
 
Field Summary
protected static String defdfmt
           
protected static String defefmt
           
protected  DecimalFormat dfmt
           
protected  DecimalFormat efmt
           
static int EXPONENT_FIELD
           
protected  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(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
 
Method Summary
 void applyExponentPattern(String pattern)
           
 void applyLocalizedPattern(String pattern)
           
 void applyMantissaPattern(String pattern)
           
 void applyPattern(String pattern)
          apply this pattern
 Object clone()
           
 boolean equals(Object obj)
           
protected static Object[] extractQuotedStrings(String text)
           
 StringBuffer format(double number, StringBuffer result, FieldPosition fieldPosition)
           
 StringBuffer format(long number, StringBuffer result, FieldPosition fieldPosition)
           
 int getExpGroupingSize()
           
 int getExpMaximumIntegerDigits()
           
 int getExpMinimumIntegerDigits()
           
 String getExpNegativePrefix()
           
 String getExpNegativeSuffix()
           
 String getExpPositivePrefix()
           
 String getExpPositiveSuffix()
           
 int getGroupingSize()
           
 int getMaximumFractionDigits()
           
 int getMaximumIntegerDigits()
           
 int getMaxWidth()
           
 int getMinimumFractionDigits()
           
 int getMinimumIntegerDigits()
           
 String getNegativePrefix()
           
 String getNegativeSuffix()
           
 String getPositivePrefix()
           
 String getPositiveSuffix()
           
 boolean isDecimalMode()
           
 boolean isDecimalSeparatorAlwaysShown()
           
 boolean isExponentialMode()
           
 boolean isFlexibleMode()
           
 boolean isGroupingUsed()
           
static void main(String[] args)
           
 Number parse(String 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.
 Number parseAsDecimal(String text, ParsePosition parsePosition)
          parse the input string, interpreting it as regular decimal format.
 Number parseAsExponential(String text, ParsePosition parsePosition)
          parse the input string, interpreting it as exponential format.
 Number parseAsFlexible(String text, ParsePosition parsePosition)
          parse the input string, interpreting it as either regular decimal format or exponential format.
protected static String replaceQuotedStrings(Vector strings, Vector positions, String input, int start)
           
 void setDecimalMode()
           
 void setDecimalSeparatorAlwaysShown(boolean newValue)
           
 void setExpGroupingSize(int newValue)
           
 void setExpMaximumIntegerDigits(int newValue)
           
 void setExpMinimumIntegerDigits(int newValue)
           
 void setExpNegativePrefix(String newValue)
           
 void setExpNegativeSuffix(String newValue)
           
 void setExponentialMode()
           
 void setExpPositivePrefix(String newValue)
           
 void setExpPositiveSuffix(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(String newValue)
           
 void setNegativeSuffix(String newValue)
           
 void setParseIntegerOnly(boolean value)
           
 void setPositivePrefix(String newValue)
           
 void setPositiveSuffix(String newValue)
           
 String toPattern()
           
 
Methods inherited from class java.text.NumberFormat
format, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, hashCode, isParseIntegerOnly, parse, parseObject, setCurrency
 
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

efmt

protected DecimalFormat efmt

dfmt

protected DecimalFormat dfmt

maxwidth

protected int maxwidth

defdfmt

protected static final String defdfmt
See Also:
Constant Field Values

defefmt

protected static final String defefmt
See Also:
Constant Field Values

EXPONENT_FIELD

public static final int EXPONENT_FIELD
See Also:
Constant Field Values
Constructor Detail

ScientificFormat

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


ScientificFormat

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


ScientificFormat

public ScientificFormat(String pattern)
Creates a ScientificFormat using a given pattern


ScientificFormat

public ScientificFormat(Locale locale,
                        String pattern)
Creates a ScientificFormat using a given pattern for the specified locale

Method Detail

applyPattern

public void applyPattern(String pattern)
                  throws IllegalArgumentException
apply this pattern

Throws:
IllegalArgumentException

extractQuotedStrings

protected static Object[] extractQuotedStrings(String text)

replaceQuotedStrings

protected static String replaceQuotedStrings(Vector strings,
                                             Vector positions,
                                             String input,
                                             int start)

applyLocalizedPattern

public void applyLocalizedPattern(String pattern)

applyMantissaPattern

public void applyMantissaPattern(String pattern)

applyExponentPattern

public void applyExponentPattern(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 String getPositivePrefix()

setPositivePrefix

public void setPositivePrefix(String newValue)

getNegativePrefix

public String getNegativePrefix()

setNegativePrefix

public void setNegativePrefix(String newValue)

getPositiveSuffix

public String getPositiveSuffix()

setPositiveSuffix

public void setPositiveSuffix(String newValue)

getNegativeSuffix

public String getNegativeSuffix()

setNegativeSuffix

public void setNegativeSuffix(String newValue)

getExpPositivePrefix

public String getExpPositivePrefix()

setExpPositivePrefix

public void setExpPositivePrefix(String newValue)

getExpNegativePrefix

public String getExpNegativePrefix()

setExpNegativePrefix

public void setExpNegativePrefix(String newValue)

getExpPositiveSuffix

public String getExpPositiveSuffix()

setExpPositiveSuffix

public void setExpPositiveSuffix(String newValue)

getExpNegativeSuffix

public String getExpNegativeSuffix()

setExpNegativeSuffix

public void setExpNegativeSuffix(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(Object obj)
Overrides:
equals in class NumberFormat

clone

public Object clone()
Overrides:
clone in class NumberFormat

toPattern

public String toPattern()

format

public StringBuffer format(double number,
                           StringBuffer result,
                           FieldPosition fieldPosition)
Specified by:
format in class NumberFormat

format

public StringBuffer format(long number,
                           StringBuffer result,
                           FieldPosition fieldPosition)
Specified by:
format in class NumberFormat

parse

public Number parse(String 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.

Specified by:
parse in class NumberFormat

parseAsExponential

public Number parseAsExponential(String 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 Number parseAsDecimal(String text,
                             ParsePosition parsePosition)
parse the input string, interpreting it as regular decimal format. This behaves just as DecimalFormat.parse().


parseAsFlexible

public Number parseAsFlexible(String 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 NumberFormat

isGroupingUsed

public boolean isGroupingUsed()
Overrides:
isGroupingUsed in class NumberFormat

setGroupingUsed

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

getMaximumIntegerDigits

public int getMaximumIntegerDigits()
Overrides:
getMaximumIntegerDigits in class NumberFormat

setMaximumIntegerDigits

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

getMinimumIntegerDigits

public int getMinimumIntegerDigits()
Overrides:
getMinimumIntegerDigits in class NumberFormat

setMinimumIntegerDigits

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

getMaximumFractionDigits

public int getMaximumFractionDigits()
Overrides:
getMaximumFractionDigits in class NumberFormat

setMaximumFractionDigits

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

getMinimumFractionDigits

public int getMinimumFractionDigits()
Overrides:
getMinimumFractionDigits in class NumberFormat

setMinimumFractionDigits

public void setMinimumFractionDigits(int newValue)
Overrides:
setMinimumFractionDigits in class 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(String[] args)


Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.