Commit ef7e8cfc authored by zhang_z's avatar zhang_z

修改计算器;

parent bcadfa76
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
<words> <words>
<w>baozheng</w> <w>baozheng</w>
<w>caocao</w> <w>caocao</w>
<w>exps</w>
<w>guanyu</w> <w>guanyu</w>
<w>huanggai</w> <w>huanggai</w>
<w>inputer</w> <w>inputer</w>
<w>likui</w> <w>likui</w>
<w>mawu</w> <w>mawu</w>
<w>patt</w>
<w>xishi</w> <w>xishi</w>
<w>zhangfei</w> <w>zhangfei</w>
<w>zhouyu</w> <w>zhouyu</w>
......
...@@ -21,12 +21,12 @@ public class PaymentFragment extends BaseFragment<PaymentPresenter, FragmentPaym ...@@ -21,12 +21,12 @@ public class PaymentFragment extends BaseFragment<PaymentPresenter, FragmentPaym
public void initView() { public void initView() {
loadRootFragment(R.id.f_inputer, mCalculatorView); loadRootFragment(R.id.f_inputer, mCalculatorView);
mCalculatorView.setmOnResultChangeListener((exp, result) -> { mCalculatorView.setOnResultChangeListener((exp, result) -> {
LogUtils.e(exp + "\n" + result); LogUtils.e(exp + "\n" + result);
mViewBinding.tvExp.setText(exp); mViewBinding.tvExp.setText(exp);
mViewBinding.tvResult.setText(ConvertUtil.fenToYuan(result)); mViewBinding.tvResult.setText(ConvertUtil.fenToYuan(result));
}); });
mCalculatorView.setmOnSettleClickListener(result -> { mCalculatorView.setOnSettleClickListener(result -> {
LogUtils.e(ConvertUtil.fenToYuan(result)); LogUtils.e(ConvertUtil.fenToYuan(result));
}); });
} }
......
...@@ -64,20 +64,16 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -64,20 +64,16 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
String[] mExps = mExp.split("[+\\-×÷]"); String[] mExps = mExp.split("[+\\-×÷]");
if (mExps.length > 1) mValue = mExps[mExps.length - 1]; if (mLast.matches("[+\\-×÷]")) mValue = "";
else mValue = mExp; else if (mExps.length > 1) mValue = mExps[mExps.length - 1];
else mValue = mExp.replaceAll("[+\\-×÷]", "");
if (mLast.matches("[+\\-×÷.]") && str.matches("[+\\-×÷.]")) return; if (str.matches("[0-9]+") && mValue.split("[.]").length > 1 && mValue.split("[.]")[1].length() == 2)
return;
if ((mExp.length() == 0 || mValue.contains(".")) && str.matches("[.]"))
return;
if (mValue.length() > 0 && str.matches("[+\\-×÷]")) {
mValue = "";
} else {
if (mValue.length() == 0 && str.matches("[+\\-×÷]")) return;
if (mValue.contains(".") && str.matches("[+\\-×÷.]")) return;
if (mValue.split("[.]").length > 1 && mValue.split("[.]")[1].length() > 2)
return;
if (mValue.length() == 0 && str.equals(".")) mExp += "0";
}
mLast = str; mLast = str;
mExp += str; mExp += str;
...@@ -97,7 +93,8 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -97,7 +93,8 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
private void delete() { private void delete() {
if (mExp.length() > 0) { if (mExp.length() > 0) {
mExp = mExp.substring(0, mExp.length() - 1); mExp = mExp.substring(0, mExp.length() - 1);
mLast = mExp.substring(mExp.length() - 1, mExp.length()); if (mExp.length() > 0) mLast = mExp.substring(mExp.length() - 1, mExp.length());
else mLast = "";
} }
} }
...@@ -196,11 +193,11 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -196,11 +193,11 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
} }
public void setmOnResultChangeListener(OnResultChangeListener mOnResultChangeListener) { public void setOnResultChangeListener(OnResultChangeListener onResultChangeListener) {
this.mOnResultChangeListener = mOnResultChangeListener; this.mOnResultChangeListener = onResultChangeListener;
} }
public void setmOnSettleClickListener(onSettleClickListener mOnSettleClickListener) { public void setOnSettleClickListener(onSettleClickListener onSettleClickListener) {
this.mOnSettleClickListener = mOnSettleClickListener; this.mOnSettleClickListener = onSettleClickListener;
} }
} }
...@@ -71,7 +71,8 @@ public class ConvertUtil { ...@@ -71,7 +71,8 @@ public class ConvertUtil {
*/ */
public static Long yuanToFen(String yuan) { public static Long yuanToFen(String yuan) {
yuan = StringUtils.isEmpty(yuan) ? "0" : yuan; yuan = StringUtils.isEmpty(yuan) ? "0" : yuan;
return (long) (Double.valueOf(yuan) * 100); BigDecimal b = new BigDecimal(Double.valueOf(yuan) * 100);
return b.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
} }
/** /**
...@@ -95,11 +96,11 @@ public class ConvertUtil { ...@@ -95,11 +96,11 @@ public class ConvertUtil {
if (l == null) return "0"; if (l == null) return "0";
return String.valueOf(l); return String.valueOf(l);
} }
/** /**
* 分转元去掉.0 ROUND_DOWN * 分转元去掉.0 ROUND_DOWN
* *
* @param fen * @param fen 分
* 分
* @return 元 * @return 元
*/ */
public static String fenToYuanNoZero(Long fen) { public static String fenToYuanNoZero(Long fen) {
...@@ -121,13 +122,11 @@ public class ConvertUtil { ...@@ -121,13 +122,11 @@ public class ConvertUtil {
/** /**
* 分转元保留2位小數 * 分转元保留2位小數
* *
* @param fen * @param fen 分
* 分
* @return 元 * @return 元
*/ */
public static String fenToYuan2(Long fen) { public static String fenToYuan2(Long fen) {
if (fen==null) if (fen == null) {
{
return "0.00"; return "0.00";
} }
BigDecimal decimal = new BigDecimal(fen); BigDecimal decimal = new BigDecimal(fen);
...@@ -137,8 +136,7 @@ public class ConvertUtil { ...@@ -137,8 +136,7 @@ public class ConvertUtil {
/** /**
* 折扣率转换最大100 * 折扣率转换最大100
* *
* @param dis * @param dis 分
* 分
* @return 元 * @return 元
*/ */
public static String discount(Long dis) { public static String discount(Long dis) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment