/**
* 获取操作数
*
* @return 操作数
*/
public BigDecimal getNum() {
BigDecimal d = new BigDecimal(jTextField.getText());
return d;
}
public void setFormatNum(BigDecimal dd) {
MathContext mc = new MathContext(18, RoundingMode.HALF_DOWN);
BigDecimal d = new BigDecimal(dd.toString(), mc);
String num = d.toString();
if (num.endsWith(".0"))
num = num.replaceAll("\\.0", "");
this.jTextField.setText(num);
}
/**
* 计算
*
* @param a1
* 操作数1
* @param c
* 操作符
* @param a2
* 操作数2
* @return
*/
public BigDecimal operate(BigDecimal a1, char c, BigDecimal a2) {
MathContext mc = new MathContext(10, RoundingMode.HALF_DOWN);
switch (c) {
case '+':
return a1.add(a2);
case '-':
return a1.subtract(a2);
case '*':
return a1.multiply(a2);
case '/':
if (a2.compareTo(new BigDecimal("0")) == 0) {
init();
return new BigDecimal("0");
}
return a1.divide(a2, mc);
default:
return a2;
}
}
}