I want to do some simple sums with some currency values expressed in BigDe开发者_开发问答cimal
type.
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 BigDecimal
arithmetics, see output behind.
Test
0
0
0
Can anyone help me out?
The BigDecimal
is immutable so you need to do this:
BigDecimal result = test.add(new BigDecimal(30));
System.out.println(result);
It looks like from the Java docs here that add returns a new 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);
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);
BigInteger is immutable, you need to do this,
BigInteger sum = test.add(new BigInteger(30));
System.out.println(sum);
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)
//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
BigDecimal no = new BigDecimal(10); //you can add like this also
no = no.add(new BigDecimal(10));
System.out.println(no);
20
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
BigInteger is immutable, just like Strings. That means we cannot change it's content once the object is created, but we can re-assign it.
BigInteger sum = test.add(new BigInteger(30));
System.out.println(sum);
.add() will return a new Object
Using Java8 lambdas
List<BigDecimal> items = Arrays.asList(a, b, c, .....);
items.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
This covers cases where the some or all of the objects in the list is null.
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.
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:
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());
}
}
精彩评论