public class SignificantFigures
extends java.lang.Number
When parsing a number to determine the number of significant figures, these rules are used:
When rounding a number the following rules are used:
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);
Constructor and Description |
---|
SignificantFigures(byte number)
Create a SignificantFigures object from a byte.
|
SignificantFigures(double number)
Create a SignificantFigures object from a double.
|
SignificantFigures(float number)
Create a SignificantFigures object from a float.
|
SignificantFigures(int number)
Create a SignificantFigures object from an integer.
|
SignificantFigures(long number)
Create a SignificantFigures object from a long.
|
SignificantFigures(java.lang.Number number)
Create a SignificantFigures object from a java number such as
a BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, or
Short.
|
SignificantFigures(short number)
Create a SignificantFigures object from a short.
|
SignificantFigures(java.lang.String number)
Create a SignificantFigures object from a String representation of a number.
|
Modifier and Type | Method and Description |
---|---|
byte |
byteValue()
Returns the value of this number as a byte.
|
double |
doubleValue()
Returns the value of this number as a double.
|
float |
floatValue()
Returns the value of this number as a float.
|
static java.lang.String |
format(byte number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(double number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(float number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(int number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(long number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(java.lang.Number number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(short number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
static java.lang.String |
format(java.lang.String number,
int significantFigures)
Convenience method to display a number with the correct
significant digits.
|
int |
getLSD()
Get the decimal place of the least significant digit.
|
int |
getMSD()
Get the decimal place of the most significant digit.
|
int |
getNumberSignificantFigures()
Get the number of significant digits.
|
int |
intValue()
Returns the value of this number as a int.
|
long |
longValue()
Returns the value of this number as a long.
|
SignificantFigures |
setLMSD(int leastPlace,
int mostPlace)
Adjust the number of significant figures such that the least
significant digit is at the given place.
|
SignificantFigures |
setLSD(int place)
Adjust the number of significant figures such that the least
significant digit is at the given place.
|
SignificantFigures |
setNumberSignificantFigures(int significantFigures)
Adjust the number of digits in the number.
|
short |
shortValue()
Returns the value of this number as a short.
|
java.lang.String |
toScientificNotation()
Formats this number in scientific notation.
|
java.lang.String |
toString()
Formats this number.
|
public SignificantFigures(java.lang.String number) throws java.lang.NumberFormatException
number
- String representation of the number.java.lang.NumberFormatException
- if the String is not a valid number.public SignificantFigures(byte number)
number
- an 8 bit integer.public SignificantFigures(short number)
number
- a 16 bit integer.public SignificantFigures(int number)
number
- a 32 bit integer.public SignificantFigures(long number)
number
- a 64 bit integer.public SignificantFigures(float number)
number
- a 32 bit floating point.public SignificantFigures(double number)
number
- a 64 bit floating point.public SignificantFigures(java.lang.Number number)
number
- a number.public int getNumberSignificantFigures()
If this number is not a number or infinity zero will be returned.
public SignificantFigures setLSD(int place)
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.
place
- the desired place of the least significant digit.public SignificantFigures setLMSD(int leastPlace, int mostPlace)
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.
leastPlace
- the desired place of the least significant digit or Integer.MIN_VALUE to ignore.mostPlace
- the desired place of the most significant digit or Integer.MIN_VALUE to ignore.public int getLSD()
If this number is not a number or infinity Integer.MIN_VALUE will be returned.
public int getMSD()
If this number is not a number or infinity Integer.MIN_VALUE will be returned.
public java.lang.String toString()
A string such as "NaN" or "Infinity" may be returned by this method.
toString
in class java.lang.Object
public java.lang.String toScientificNotation()
A string such as "NaN" or "Infinity" may be returned by this method.
public SignificantFigures setNumberSignificantFigures(int significantFigures)
This method has no effect if this number is not a number or infinity.
significantFigures
- desired number of significant figures.public byte byteValue() throws java.lang.NumberFormatException
byteValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a byte.public double doubleValue() throws java.lang.NumberFormatException
doubleValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a double.public float floatValue() throws java.lang.NumberFormatException
floatValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a float.public int intValue() throws java.lang.NumberFormatException
intValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a int.public long longValue() throws java.lang.NumberFormatException
longValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a long.public short shortValue() throws java.lang.NumberFormatException
shortValue
in class java.lang.Number
java.lang.NumberFormatException
- if this number cannot be converted to a short.public static java.lang.String format(byte number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(double number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(float number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(int number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(long number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(java.lang.Number number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(short number, int significantFigures)
number
- the number to displaysignificantFigures
- the number of significant figures to display.public static java.lang.String format(java.lang.String number, int significantFigures) throws java.lang.NumberFormatException
number
- the number to displaysignificantFigures
- the number of significant figures to display.java.lang.NumberFormatException
- if the String is not a valid number.Copyright (c) 2001-2020 by Stephen Ostermiller