| java.lang.Object java.lang.Number com.Ostermiller.util.SignificantFigures
SignificantFigures | public class SignificantFigures extends Number (Code) | | A number with an associated number of significant figures.
This class handles parsing numbers, determining the number
of significant figures, adjusting the number of significant
figures (including scientific rounding), and displaying the number.
More information about this class is available from ostermiller.org.
When parsing a number to determine the number of significant figures,
these rules are used:
- Non-zero digits are always significant.
- All zeros between other significant digits are significant.
- All zeros left of the decimal point between a significant digit and the decimal point are significant.
- All trailing zeros to the right of the decimal point are significant.
- If the number is contains no digits other than zero, every zero is significant.
When rounding a number the following rules are used:
- If the greatest insignificant digit is less than five, round down.
- If the greatest insignificant digit is greater than five, round up.
- If the greatest insignificant digit is five and followed by some non-zero digit, round up.
- If the greatest insignificant digit is five and followed only by zeros, and the least significant
digit is odd, round up.
- If the greatest insignificant digit is five and followed only by zeros, and the least significant
digit is even, round down.
Example of using this class to multiply numbers and display the result
with the proper number of significant figures:
String[] arguments = {"1.0", "2.0", ...}
SignificantFigures number;
int sigFigs = Integer.MAX_VALUE;
double result = 1D;
for (int i=0; i<arguments.length; i++){
number = new SignificantFigures(arguments[i]);
sigFigs = Math.min(sigFigs, number.getNumberSignificantFigures());
result *= number.doubleValue();
}
number = new SignificantFigures(result);
number.setNumberSignificantFigures(sigFigs);
System.out.println(number);
Example of using this class to add numbers and display the result
with the proper number of significant figures:
String[] arguments = {"1.0", "2.0", ...}
SignificantFigures number;
int leastSD = Integer.MIN_VALUE;
int mostSD = Integer.MIN_VALUE;
double result = 0D;
for (int i=0; i<arguments.length; i++){
number = new SignificantFigures(arguments[i]);
leastSD = Math.max(leastSD, number.getLSD());
mostSD = Math.max(mostSD, number.getMSD());
result += number.doubleValue();
}
number = new SignificantFigures(result);
number.setLMSD(leastSD, mostSD);
System.out.println(number);
author: Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities since: ostermillerutils 1.00.00 |
Constructor Summary | |
public | SignificantFigures(String number) Create a SignificantFigures object from a String representation of a number. | public | SignificantFigures(byte number) Create a SignificantFigures object from a byte. | public | SignificantFigures(short number) Create a SignificantFigures object from a short. | public | SignificantFigures(int number) Create a SignificantFigures object from an integer. | public | SignificantFigures(long number) Create a SignificantFigures object from a long. | public | SignificantFigures(float number) Create a SignificantFigures object from a float. | public | SignificantFigures(double number) Create a SignificantFigures object from a double. | public | SignificantFigures(Number number) Create a SignificantFigures object from a java number such as
a BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, or
Short. |
Method Summary | |
public byte | byteValue() Returns the value of this number as a byte. | public double | doubleValue() Returns the value of this number as a double. | public float | floatValue() Returns the value of this number as a float. | public static String | format(byte number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(double number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(float number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(int number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(long number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(Number number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(short number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public static String | format(String number, int significantFigures) Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. | public int | getLSD() Get the decimal place of the least significant digit. | public int | getMSD() Get the decimal place of the most significant digit. | public int | getNumberSignificantFigures() Get the number of significant digits. | public int | intValue() Returns the value of this number as a int. | public long | longValue() Returns the value of this number as a long. | public SignificantFigures | setLMSD(int leastPlace, int mostPlace) Adjust the number of significant figures such that the least
significant digit is at the given place. | public SignificantFigures | setLSD(int place) Adjust the number of significant figures such that the least
significant digit is at the given place. | public SignificantFigures | setNumberSignificantFigures(int significantFigures) Adjust the number of digits in the number.
Pad the tail with zeros if too short, round the
number according to scientific rounding if too long, leave alone
if just right.
This method has no effect if this number is not a number or infinity.
Parameters: significantFigures - desired number of significant figures. | public short | shortValue() Returns the value of this number as a short. | public String | toScientificNotation() Formats this number in scientific notation. | public String | toString() Formats this number. |
SignificantFigures | public SignificantFigures(String number) throws NumberFormatException(Code) | | Create a SignificantFigures object from a String representation of a number.
Parameters: number - String representation of the number. throws: NumberFormatException - if the String is not a valid number. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(byte number)(Code) | | Create a SignificantFigures object from a byte.
Parameters: number - an 8 bit integer. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(short number)(Code) | | Create a SignificantFigures object from a short.
Parameters: number - a 16 bit integer. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(int number)(Code) | | Create a SignificantFigures object from an integer.
Parameters: number - a 32 bit integer. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(long number)(Code) | | Create a SignificantFigures object from a long.
Parameters: number - a 64 bit integer. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(float number)(Code) | | Create a SignificantFigures object from a float.
Parameters: number - a 32 bit floating point. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(double number)(Code) | | Create a SignificantFigures object from a double.
Parameters: number - a 64 bit floating point. since: ostermillerutils 1.00.00 |
SignificantFigures | public SignificantFigures(Number number)(Code) | | Create a SignificantFigures object from a java number such as
a BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, or
Short.
Parameters: number - a number. since: ostermillerutils 1.00.00 |
byteValue | public byte byteValue() throws NumberFormatException(Code) | | Returns the value of this number as a byte.
the numeric value represented by this object after conversion to type byte. throws: NumberFormatException - if this number cannot be converted to a byte. since: ostermillerutils 1.00.00 |
doubleValue | public double doubleValue() throws NumberFormatException(Code) | | Returns the value of this number as a double.
the numeric value represented by this object after conversion to type double. throws: NumberFormatException - if this number cannot be converted to a double. since: ostermillerutils 1.00.00 |
floatValue | public float floatValue() throws NumberFormatException(Code) | | Returns the value of this number as a float.
the numeric value represented by this object after conversion to type float. throws: NumberFormatException - if this number cannot be converted to a float. since: ostermillerutils 1.00.00 |
format | public static String format(byte number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(double number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(float number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(int number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(long number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(Number number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(short number, int significantFigures)(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures since: ostermillerutils 1.02.07 |
format | public static String format(String number, int significantFigures) throws NumberFormatException(Code) | | Convenience method to display a number with the correct
significant digits.
Parameters: number - the number to display Parameters: significantFigures - the number of significant figures to display. the number formatted with the correct significant figures throws: NumberFormatException - if the String is not a valid number. since: ostermillerutils 1.02.07 |
getLSD | public int getLSD()(Code) | | Get the decimal place of the least significant digit.
If this number is not a number or infinity Integer.MIN_VALUE
will be returned.
the decimal place of the least significant digit. since: ostermillerutils 1.00.00 |
getMSD | public int getMSD()(Code) | | Get the decimal place of the most significant digit.
If this number is not a number or infinity Integer.MIN_VALUE
will be returned.
the decimal place of the least significant digit. since: ostermillerutils 1.00.00 |
getNumberSignificantFigures | public int getNumberSignificantFigures()(Code) | | Get the number of significant digits.
If this number is not a number or infinity zero
will be returned.
the number of significant digits in this number. since: ostermillerutils 1.00.00 |
intValue | public int intValue() throws NumberFormatException(Code) | | Returns the value of this number as a int.
the numeric value represented by this object after conversion to type int. throws: NumberFormatException - if this number cannot be converted to a int. since: ostermillerutils 1.00.00 |
longValue | public long longValue() throws NumberFormatException(Code) | | Returns the value of this number as a long.
the numeric value represented by this object after conversion to type long. throws: NumberFormatException - if this number cannot be converted to a long. since: ostermillerutils 1.00.00 |
setLMSD | public SignificantFigures setLMSD(int leastPlace, int mostPlace)(Code) | | Adjust the number of significant figures such that the least
significant digit is at the given place. This method may add
significant zeros to the end of this number, or remove significant
digits from this number.
If all significant digits are removed from this number by truncating to
the least significant place, a zero will be created with significant figures
from the least to most significant places.
This method has no effect if this number is not a number or infinity.
Parameters: leastPlace - the desired place of the least significant digit or Integer.MIN_VALUE to ignore. Parameters: mostPlace - the desired place of the most significant digit or Integer.MIN_VALUE to ignore. this number since: ostermillerutils 1.00.00 |
setLSD | public SignificantFigures setLSD(int place)(Code) | | Adjust the number of significant figures such that the least
significant digit is at the given place. This method may add
significant zeros to the end of this number, or remove significant
digits from this number.
It is possible to remove all significant digits from this number which
will cause the string representation of this number to become "NaN". This
could become a problem if you are adding numbers and the result is close
to zero. All of the significant digits may get removed, even though the
result could be zero with some number of significant digits. Its is safes
to use the setLMSD() method which will make a zero with the appropriate
number of significant figures in such instances.
This method has no effect if this number is not a number or infinity.
Parameters: place - the desired place of the least significant digit. this number. since: ostermillerutils 1.00.00 |
setNumberSignificantFigures | public SignificantFigures setNumberSignificantFigures(int significantFigures)(Code) | | Adjust the number of digits in the number.
Pad the tail with zeros if too short, round the
number according to scientific rounding if too long, leave alone
if just right.
This method has no effect if this number is not a number or infinity.
Parameters: significantFigures - desired number of significant figures. This number. since: ostermillerutils 1.00.00 |
shortValue | public short shortValue() throws NumberFormatException(Code) | | Returns the value of this number as a short.
the numeric value represented by this object after conversion to type short. throws: NumberFormatException - if this number cannot be converted to a short. since: ostermillerutils 1.00.00 |
toScientificNotation | public String toScientificNotation()(Code) | | Formats this number in scientific notation.
A string such as "NaN" or "Infinity" may be returned by this method.
representation of this number in scientific notation. since: ostermillerutils 1.00.00 |
toString | public String toString()(Code) | | Formats this number.
If the number is less than 10^-3 or greater than or equal to 10^7,
or the number might have an ambiguous number of significant figures,
scientific notation will be used.
A string such as "NaN" or "Infinity" may be returned by this method.
representation of this number. since: ostermillerutils 1.00.00 |
|
|