[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding Decimal Values

Posted on 2011-10-11
5
Medium Priority
?
173 Views
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
0
Comment
Question by:deharvy
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36950000
Don't use that ctor for BigDecimal - it defeats the object. Use BigDecimal(String number)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36950004
>>and the value should be 1.39061

Use can use

bd = bd.setScale(...)

to set it to whatever you want
0
 

Author Closing Comment

by:deharvy
ID: 36950188
Thank you!
0
 
LVL 4

Expert Comment

by:stachenov
ID: 36950228
>>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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36950485
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 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