如何计算BigDecimal

时间:2016-10-23 07:48:49

标签: java

我创建了这个JUnit测试,以测试如何计算值为BigDecimal的Objects的所有值。

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Test;

public class BigDecimalTest
{
    private BigDecimal sumAmounts(List<ExpressCheckout> myList)
    {
        BigDecimal total = new BigDecimal(BigInteger.ZERO);
        for (ExpressCheckout item : myList)
        {
            total.add(item.getAmount());
        }
        return total;
    }

    @Test
    public void testBigDecimalTest()
    {
        try
        {
            List<ExpressCheckout> list = new ArrayList<>();
            for (int i = 0; i < 12; i++)
            {
                ExpressCheckout obj = new ExpressCheckout();
                obj.setCurrency("USD");

                Random rand = new Random();

                int n = rand.nextInt(50) + 1;
                obj.setAmount(new BigDecimal(n));
                obj.setQuantity(1);
                obj.setName("test name");
                obj.setDescription("test description");
                list.add(obj);
            }

            BigDecimal sumAmounts = sumAmounts(list);

            System.out.println(">>>>>>>>>>>>>>>>>>>> sumAmounts " + sumAmounts.toString());

        }
        catch (Exception ex)
        {
            Logger.getLogger(BigDecimalTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public class ExpressCheckout
    {
        String currency;
        BigDecimal amount;
        int quantity;
        String name;
        String description;

        public ExpressCheckout()
        {
        }

        public ExpressCheckout(String currency, BigDecimal amount, int quantity, String name, String description)
        {
            this.currency = currency;
            this.amount = amount;
            this.quantity = quantity;
            this.name = name;
            this.description = description;
        }

        public String getCurrency()
        {
            return currency;
        }

        public void setCurrency(String currency)
        {
            this.currency = currency;
        }

        public BigDecimal getAmount()
        {
            return amount;
        }

        public void setAmount(BigDecimal amount)
        {
            this.amount = amount;
        }

        public int getQuantity()
        {
            return quantity;
        }

        public void setQuantity(int quantity)
        {
            this.quantity = quantity;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public String getDescription()
        {
            return description;
        }

        public void setDescription(String description)
        {
            this.description = description;
        }
    }

}

出于某种原因,我得到结果sumAmounts 0知道为什么我得到这个结果以及如何解决它? 结果应该从Java List中的所有BigDecimal值计算。

1 个答案:

答案 0 :(得分:3)

java中的

BigDecimal是不可变的,因此您在sumAmounts方法中的语句如下:

total.add(item.getAmount());

需要更改为:

total = total.add(item.getAmount());
相关问题