Solved

Add two BigDecimal values

Posted on 2007-11-22
4
1,306 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
Comment Utility
try:

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

Expert Comment

by:UrosVidojevic
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

6 Experts available now in Live!

Get 1:1 Help Now