Significant Figures

A Java number class for figuring out how many significant figures each of the numbers in a calculation has and displaying the result appropriately.

Example

Multiplying numbers using significant figures.

// Numbers to multiply
String[] args = {"1.0", "2.0"};
SignificantFigures number;
int sigs = Integer.MAX_VALUE;
double result = 1D;
for (int i = 0; i < args.length; i++){
    // For each number, figure out significant figures
    // and multiply the numbers
    number = new SignificantFigures(args[i]);
    sigs = Math.min(sigs, number.getNumberSignificantFigures());
    result *= number.doubleValue();
}
// format and display the result
number = new SignificantFigures(result);
number.setNumberSignificantFigures(sigs);
System.out.println(number);

Example

Adding numbers using significant figures.

// Numbers to add
String[] args = {"1.0", "2.0"};
SignificantFigures number;
int lsd = Integer.MIN_VALUE;
int msd = Integer.MIN_VALUE;
double result = 0D;
for (int i = 0; i < args.length; i++){
    // for each number figure most and least
    // significant digit then add the number.
    number = new SignificantFigures(args[i]);
    lsd = Math.max(lsd, number.getLSD());
    msd = Math.max(msd, number.getMSD());
    result += number.doubleValue();
}
// format and display the result
number = new SignificantFigures(result);
number.setLMSD(lsd, msd);
System.out.println(number);