/**
* Created on 2008-01-05
*/
package simulator.utils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Locale;
/**
* This class provides basic mathematical operations for the double
primitive type
* @author Stanislaw Szczepanowski
*/
public class DoubleMath {
/**
* The pattern used for converting a double
value to a string
*/
public static String PATTERN = "###.###";
/**
* A protected constructor which will be invoked only
* one time in the lifetime of this class
*/
protected DoubleMath() {
}
/**
* Adds two double
values and returns the result of addition
* @param left the left argument
* @param right the right argument
* @return the result of addition of the two arguments
*/
public static double add(double left, double right) {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
df.applyPattern(PATTERN);
String leftStr = df.format(left);
String rightStr = df.format(right);
BigDecimal resultDec = new BigDecimal(leftStr);
BigDecimal rightDec = new BigDecimal(rightStr);
resultDec = resultDec.add(rightDec);
return resultDec.doubleValue();
}
/**
* Subtracts from the left
argument the right
argument and returns the result of subtraction
* @param left the left argument
* @param right the right argument
* @return the result of subtraction of the two arguments
*/
public static double subtract(double left, double right) {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
df.applyPattern(PATTERN);
String leftStr = df.format(left);
String rightStr = df.format(right);
BigDecimal resultDec = new BigDecimal(leftStr);
BigDecimal rightDec = new BigDecimal(rightStr);
resultDec = resultDec.subtract(rightDec);
return resultDec.doubleValue();
}
}