Addition for BigDecimal
The BigDecimal
is immutable so you need to do this:
BigDecimal result = test.add(new BigDecimal(30));
System.out.println(result);
BigDecimal += (add and assign) ...How to do this?
There is an add
method in the BigDecimal
class.
You would have to do - a = a.add(b);
Have a look at the java docs.
BigDecimal.add() being ignored
That's answered by looking at the docs for BigDecimal
Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).
Emphasis mine. So add
doesn't modify the existing BigDecimal
- it can't, since BigDecimal
s are immutable. According to the docs, BigDecimal
s are
Immutable, arbitrary-precision signed decimal numbers.
Instead of modifying its value, it returns a new value which is equal to the result of the addition.
Change this:
totalPrice.add(withTax, new MathContext(5));
to this:
totalPrice = totalPrice.add(withTax, new MathContext(5));
to assign that new value back to the same variable, and it will correctly update like you expect.
Compare that to this line:
withTax = withoutTax.add(tax, new MathContext(5));
You wouldn't expect the value of withoutTax
to change simply because you used it in a calculation. In order for that line to work as expected, the add
method cannot be allowed to modify the object it is called on.
adding 2 BigDecimal values
BigDecimal is immutable. Every operation returns a new instance containing the result of the operation:
BigDecimal sum = x.add(y);
If you want x to change, you thus have to do
x = x.add(y);
Reading the javadoc really helps understanding how a class and its methods work.
How Can I perform Addition on Big Decimal?
Looks like you are trying to avoid 1E-10
expression
you can do that as
BigDecimal bigD = new BigDecimal("0.0000000000").add(new BigDecimal("0.0000000001"));
System.out.println(bigD.toPlainString());
adding values from a BigDecimal [ ]
Assuming that you are adding an array of Strings
containing valid numbers you could do this:
BigDecimal sum = BigDecimal.ZERO;
for (int i = 0; i < select.length; i++)
{
sum = sum.add(new BigDecimal(select[i]));
}
out.println(sum);
The array total[]
is pretty much redundant. You can move your sum
declaration and out.println(sum);
out of your loop.
Related Topics
Why Do We Need Immutable Class
How to Format a Java String with Leading Zero
Safe String to Bigdecimal Conversion
What Is the Breakdown for Java's Lambda Syntax
Coding Conventions - Naming Enums
Is the Order Guaranteed for the Return of Keys and Values from a Linkedhashmap Object
Using Gzip Compression with Spring Boot/Mvc/Javaconfig with Restful
The Meaning of Noinitialcontextexception Error
Synchronizing on an Integer Value
Is a Java Array of Primitives Stored in Stack or Heap
How to Convert Ascii Code (0-255) to Its Corresponding Character
Why Do Many Collection Classes in Java Extend the Abstract Class and Implement the Interface as Well
What Does It Mean to Program to an Interface
How to Move a File from One Location to Another in Java
Java Generics: Cannot Cast List<Subclass> to List<Superclass>