Solved

Add two BigDecimal values

Posted on 2007-11-22
4
1,313 Views
Last Modified: 2013-12-29
Hello,

I expected in the folowing code "totalN" to be 52.75,
thank you for any help.

BigDecimal totalN = new BigDecimal("0");
totalN.add(nR1);
System.err.println(nR1.doubleValue());      // 52.75
System.err.println(totalN.doubleValue());  // 0.0
0
Comment
Question by:matthew016
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 380 total points
ID: 20335553
try:

totalN = totalN.add(nR1);
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20335573
From java documentation:

add

public BigDecimal add(BigDecimal val)

    Returns a BigDecimal whose value is (this + val), and whose scale is max(this.scale(), val.scale()).

    Parameters:
        val - value to be added to this BigDecimal.
    Returns:
        this + val

In other words number1.add(number2) doesn't modify number1 object!
0
 
LVL 9

Assisted Solution

by:ysnky
ysnky earned 20 total points
ID: 20335616
you have to catch add operation result. try this;

            BigDecimal nR1 = new BigDecimal(52.75);
            BigDecimal totalN = new BigDecimal("0");
            totalN = totalN.add(nR1);

            System.err.println(nR1.doubleValue());      
            System.err.println(totalN.doubleValue());
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 100 total points
ID: 20336285
have a look at this one to get a more understanding on why..
http://www.javaranch.com/journal/2003/04/immutable.htm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 68
recursion example 16 111
Java DateChooser? 3 29
servlet doXXX methods 3 36
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now