Adding Decimal Values

Posted on 2011-10-11
Last Modified: 2012-05-12

Using java, I'm getting mixed results when trying to add numbers with decimals. Whether I try BigDecimal or Double, I get strange behavior.

BigDecimal test = new BigDecimal(doubleValue);
double test2 = doubleValue;

doubleValue is: 1.3906

For both methods,

Sometimes I get 1.3906
Other times I get: 1.3906099999999999017319396443781442940235137939453125

I don't want to limit the decimal places, because I might want to add something like 0.00001 later on and the value should be 1.39061
Question by:deharvy
    LVL 86

    Accepted Solution

    Don't use that ctor for BigDecimal - it defeats the object. Use BigDecimal(String number)
    LVL 86

    Expert Comment

    >>and the value should be 1.39061

    Use can use

    bd = bd.setScale(...)

    to set it to whatever you want

    Author Closing Comment

    Thank you!
    LVL 4

    Expert Comment

    >>Other times I get: 1.3906099999999999017319396443781442940235137939453125

    This is too much of a difference! You get nonsense somewhere past 16-17 digits or so, that's expected since doubles by their nature can't store any numbers that can't be represented as integers multiplied by some power of 2 (so you can store 0.5, but not 0.2). However, that doesn't explain all those 9s you have there. It looks more like a valid representation of 1.39061, not 1.3906. Are you sure that double value is supposed to be 1.3906 in both cases?

    If you need exact results, you should avoid doubles (or floats) completely.
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    how to use external config file with Spring MVC 4 36
    Updating Java 9 62
    wordlen challenge 3 26
    @SBGen Method 3 16
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    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 …

    758 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

    8 Experts available now in Live!

    Get 1:1 Help Now