Java BigDecimal 的加法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1846900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:16:37  来源:igfitidea点击:

Addition for BigDecimal

javabigdecimal

提问by Sergio del Amo

I want to do some simple sums with some currency values expressed in BigDecimaltype.

我想用一些用BigDecimal类型表示的货币值做一些简单的求和。

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test.add(new BigDecimal(30));
System.out.println(test);
test.add(new BigDecimal(45));
System.out.println(test);

Obviously I do not understand well the BigDecimalarithmetics, see output behind.

显然我对BigDecimal算术不太了解,请参阅后面的输出。

Test
0
0
0

Can anyone help me out?

谁能帮我吗?

采纳答案by Vincent Ramdhanie

The BigDecimalis immutable so you need to do this:

BigDecimal所以你需要做的,这是不变的:

BigDecimal result = test.add(new BigDecimal(30));
System.out.println(result);

回答by ZZ Coder

BigInteger is immutable, you need to do this,

BigInteger 是不可变的,你需要这样做,

  BigInteger sum = test.add(new BigInteger(30));  
  System.out.println(sum);

回答by Maurice Perry

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test = test.add(new BigDecimal(30));
System.out.println(test);
test = test.add(new BigDecimal(45));
System.out.println(test);

回答by Ankur Goel

It looks like from the Java docs herethat add returns a new BigDecimal:

这里的 Java 文档看来,add 返回一个新的 BigDecimal:

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test = test.add(new BigDecimal(30));
System.out.println(test);
test = test.add(new BigDecimal(45));
System.out.println(test);

回答by nfechner

It's actually rather easy. Just do this:

其实还是比较容易的。只需这样做:

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test = test.add(new BigDecimal(30));
System.out.println(test);
test = test.add(new BigDecimal(45));
System.out.println(test);

See also: BigDecimal#add(java.math.BigDecimal)

也可以看看: BigDecimal#add(java.math.BigDecimal)

回答by MAK

//you can do in this way...as BigDecimal is immutable so cant set values except in constructor

BigDecimal test = BigDecimal.ZERO;
BigDecimal result = test.add(new BigDecimal(30));
System.out.println(result);

result would be 30

回答by Yatish

BigDecimal no = new BigDecimal(10); //you can add like this also
no = no.add(new BigDecimal(10));
System.out.println(no);


20

20

回答by snipersnack

BigDecimal demo = new BigDecimal(15);

It is immutable beacuse it internally store you input i.e (15) as final private final BigInteger intVal;and same concept use at the time of string creation every input finally store in private final char value[];.So there is no implmented bug.

它是不可变的,因为它在内部存储您的输入,即 (15) 作为 final private final BigInteger intVal;字符串创建时使用的相同概念,每个输入最终存储在 private final char value[];.所以没有实现的错误。

回答by Paolo Leoncini

Just another example to add BigDecimals. Key point is that they are immutable and they can be initialized only in the constructor. Here is the code:

只是另一个例子来添加BigDecimals. 关键是它们是不可变的,只能在构造函数中初始化。这是代码:

import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc;
        boolean first_right_number = false;
        BigDecimal initBigDecimal = BigDecimal.ZERO;
        BigDecimal add1 = BigDecimal.ZERO;
        BigDecimal add2 = BigDecimal.ZERO;

        while (!first_right_number)
        {
            System.out.print("Enter a first single numeric value: ");
            sc = new Scanner(System.in);
            if (sc.hasNextBigDecimal()) 
            {
                first_right_number = true;
                add1 = sc.nextBigDecimal();
            }
        }

        boolean second_right_number = false;
        while (!second_right_number)
        {
            System.out.print("Enter a second single numeric value: ");
            sc = new Scanner(System.in);
            if (sc.hasNextBigDecimal()) 
            {
                second_right_number = true;
                add2 = sc.nextBigDecimal();
            }
        }
        BigDecimal result = initBigDecimal.add(add1).add(add2);
        System.out.println("Sum of the 2 numbers is: " + result.toString());
    }
}

回答by Artur Krawczyk

You can also do it like this:

你也可以这样做:

BigDecimal A = new BigDecimal("10000000000");
BigDecimal B = new BigDecimal("20000000000");
BigDecimal C = new BigDecimal("30000000000");
BigDecimal resultSum = (A).add(B).add(C);
System.out.println("A+B+C= " + resultSum);

Prints:

印刷:

A+B+C= 60000000000

A+B+C= 60000000000