Package xal.tools.text
Class ScientificNumberFormat
java.lang.Object
java.text.Format
java.text.NumberFormat
xal.tools.text.ScientificNumberFormat
- All Implemented Interfaces:
Serializable
,Cloneable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.NumberFormat
NumberFormat.Field, NumberFormat.Style
-
Field Summary
Fields inherited from class java.text.NumberFormat
FRACTION_FIELD, INTEGER_FIELD
-
Constructor Summary
ConstructorsConstructorDescriptionEmpty convenience constructor defaulting to four significant digits.ScientificNumberFormat
(int significantDigits) Convenience constructor.ScientificNumberFormat
(int significantDigits, int fieldWidth) Convenience constructor.ScientificNumberFormat
(int significantDigits, int fieldWidth, boolean fixedLength) Designated constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal 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.void
appendToIO
(Appendable output, CharSequence separator, double... values) Append the formatted values to the output throwing any internal IOException from the output.format
(double number, StringBuffer inputBuffer, FieldPosition position) Implement the abstract format method by delegating to an internal number formatformat
(long number, StringBuffer inputBuffer, FieldPosition position) Implement the abstract format method by delegating to an internal number formatint
Get the field width for displaying the right justified output when used with fixed length mode.int
Get the number of significant digits to outputboolean
Determine whether the formatted output is padded with spaces to the left (right justification) as needed to fill out the fixed width.parse
(String source, ParsePosition position) Implement the abstract parse method by delegating to an internal number formatfinal void
print
(CharSequence separator, CharSequence terminator, double... values) Print the formatted valuesfinal void
println
(CharSequence separator, double... values) Convenience method to print the formatted values with the system's trailing line terminatorvoid
setFieldWidth
(int fieldWidth) Set the field width (used with fixed length mode to format output with fixed width padding with spaces as needed)void
setFixedLength
(boolean fixedLength) format values using a the field width and padding with spaces as neededvoid
setSignificantDigits
(int significantDigits) Set the number of significant digitsMethods inherited from class java.text.NumberFormat
clone, equals, format, format, format, getAvailableLocales, getCompactNumberInstance, getCompactNumberInstance, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, hashCode, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
Constructor Details
-
ScientificNumberFormat
public ScientificNumberFormat(int significantDigits, int fieldWidth, boolean fixedLength) Designated constructor.- Parameters:
significantDigits
- total number of significant digits to displayfieldWidth
- field width of right justified text to displayfixedLength
- 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 displayfieldWidth
- 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
Implement the abstract format method by delegating to an internal number format- Specified by:
format
in classNumberFormat
-
format
Implement the abstract format method by delegating to an internal number format- Specified by:
format
in classNumberFormat
-
parse
Implement the abstract parse method by delegating to an internal number format- Specified by:
parse
in classNumberFormat
-
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 valuesseparator
- 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
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 valuesseparator
- the characters to insert between each formatted value (e.g. this could be a comma)values
- the values to format and output
-
print
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
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
-