为什么我的大十进制计算不起作用?

dfddblmv  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(316)

我有一个与swing的否定问题,出于某种原因,我的大十进制否定和加法不工作,我的代码编译,但减号和加号的计算不工作,任何帮助是非常感谢。
代码段

//Convert the JLabel to a Double so we can perform negation.
diallerPanelSum =  new BigDecimal(balanceAmount.getText());

//Dont allow the Balance to go negative!
if(diallerPanelSum.compareTo(BigDecimal.ZERO)>0)
    {

        if(e.getSource()==buttonMakeCall)
        {
            diallerPanelSum.subtract(new BigDecimal("1.0"));
        }

        if(e.getSource()==buttonSendText)
        {
            diallerPanelSum.subtract(new BigDecimal("0.10"));
        }

        if(e.getSource()==buttonTopUp)
        {
            diallerPanelSum.add(new BigDecimal("10.00"));
        }

    }

//Convert the Float back to a JLabel
balanceAmount.setText(String.valueOf(diallerPanelSum));
jw5wzhpr

jw5wzhpr1#

BigDecimals 是不变的。所以你必须分配操作的结果,比如 add() 或者 subtract()BigDecimal 再一次,当它们产生一个新的 BigDecimal . 请尝试以下操作:

if (diallerPanelSum.compareTo(BigDecimal.ZERO) > 0)
{

    if (e.getSource() == buttonMakeCall)
    {
        diallerPanelSum = diallerPanelSum.subtract(BigDecimal.ONE);
    }

    if (e.getSource() == buttonSendText)
    {
        diallerPanelSum = diallerPanelSum.subtract(new BigDecimal("0.10"));
    }

    if (e.getSource() == buttonTopUp)
    {
        diallerPanelSum = diallerPanelSum.add(BigDecimal.TEN);
    }

}

相关问题