Class ScientificNumberFormat

java.lang.Object
java.text.Format
java.text.NumberFormat
xal.tools.text.ScientificNumberFormat
All Implemented Interfaces:
Serializable, Cloneable

public class ScientificNumberFormat extends NumberFormat
Formats numbers in scientific notation using the specified number of significant digits and a specified width for the space occupied by the right justified formatted output. The output always has exactly one integer digit and displays the specified number of significant digits using exponential notation as needed.
See Also:
  • Constructor Details

    • ScientificNumberFormat

      public ScientificNumberFormat(int significantDigits, int fieldWidth, boolean fixedLength)
      Designated constructor.
      Parameters:
      significantDigits - total number of significant digits to display
      fieldWidth - field width of right justified text to display
      fixedLength - specified whether to pad the formatted output to the left (right justification) as needed to fill out the field width
    • ScientificNumberFormat

      public ScientificNumberFormat(int significantDigits, int fieldWidth)
      Convenience constructor. By default, formats a value with padding to the field width (set fixedLength to change mode).
      Parameters:
      significantDigits - total number of significant digits to display
      fieldWidth - field width of right justified text to display
    • ScientificNumberFormat

      public ScientificNumberFormat(int significantDigits)
      Convenience constructor. The width is significant digits + 1 space + 1 sign + 1 decimal point + 5 for exponent field. By default, formats a value without padding to the field width (set fixedLength to change mode).
      Parameters:
      significantDigits - total number of significant digits to display
    • ScientificNumberFormat

      public ScientificNumberFormat()
      Empty convenience constructor defaulting to four significant digits. By default, formats a value without padding to the field width (set fixedLength to change mode).
  • Method Details

    • isFixedLength

      public boolean isFixedLength()
      Determine whether the formatted output is padded with spaces to the left (right justification) as needed to fill out the fixed width.
      Returns:
      true if using fixed length mode and false if not
    • setFixedLength

      public void setFixedLength(boolean fixedLength)
      format values using a the field width and padding with spaces as needed
    • getSignificantDigits

      public int getSignificantDigits()
      Get the number of significant digits to output
      Returns:
      the number of significant digits to output
    • setSignificantDigits

      public void setSignificantDigits(int significantDigits)
      Set the number of significant digits
      Parameters:
      significantDigits - total number of significant digits to display
    • getFieldWidth

      public int getFieldWidth()
      Get the field width for displaying the right justified output when used with fixed length mode.
      Returns:
      the field width
    • setFieldWidth

      public void setFieldWidth(int fieldWidth)
      Set the field width (used with fixed length mode to format output with fixed width padding with spaces as needed)
      Parameters:
      fieldWidth - field width of right justified text to display
    • format

      public StringBuffer format(double number, StringBuffer inputBuffer, FieldPosition position)
      Implement the abstract format method by delegating to an internal number format
      Specified by:
      format in class NumberFormat
    • format

      public StringBuffer format(long number, StringBuffer inputBuffer, FieldPosition position)
      Implement the abstract format method by delegating to an internal number format
      Specified by:
      format in class NumberFormat
    • parse

      public Number parse(String source, ParsePosition position)
      Implement the abstract parse method by delegating to an internal number format
      Specified by:
      parse in class NumberFormat
    • appendToIO

      public void appendToIO(Appendable output, CharSequence separator, double... values) throws IOException
      Append the formatted values to the output throwing any internal IOException from the output. Use this method when you want to handle IO Exceptions
      Parameters:
      output - the output to which to append the formatted values
      separator - the characters to insert between each formatted value (e.g. this could be a comma)
      values - the values to format and output
      Throws:
      IOException
    • appendTo

      public final void appendTo(Appendable output, CharSequence separator, double... values)
      Convenience method to append the formatted values to the output suppressing IOException and instead dumping the stack trace upon exception. This is useful when calling with outputs that don't through IOException (e.g. StringBuffer and StringBuilder)
      Parameters:
      output - the output to which to append the formatted values
      separator - the characters to insert between each formatted value (e.g. this could be a comma)
      values - the values to format and output
    • print

      public final void print(CharSequence separator, CharSequence terminator, double... values)
      Print the formatted values
      Parameters:
      separator - the characters to insert between each formatted value (e.g. this could be a comma)
      terminator - the characters to append to the end of the line of formatted values (e.g. this could be "\n")
      values - the values to format and output
    • println

      public final void println(CharSequence separator, double... values)
      Convenience method to print the formatted values with the system's trailing line terminator
      Parameters:
      separator - the characters to insert between each formatted value (e.g. this could be a comma)
      values - the values to format and output