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
Solved

Add two BigDecimal values

Posted on 2007-11-22
4
1,329 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

860 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