?
Solved

Add two BigDecimal values

Posted on 2007-11-22
4
Medium Priority
?
1,350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 1520 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 80 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

752 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