File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes wrong value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "wrong value" Watch "wrong value" New topic
Author

wrong value

Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
Hi,

I am running this formula in my java code and the output I am getting is: 8.28124999171875E8. If i put the same formula in an excel file or use calculator the output i am getting is: 828124999.17 which i believe is the correct value. What is the mistake i am making while computing the formula in my java code?
Double value = ((2500/144) * (999999999/1000)) * 47.7

All the values in the formulas are variable of type double.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Use BigDecimal insteadof Double , if you want to eliminate the E .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Those are the same value (more or less).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

seetharaman venkatasamy wrote:Use BigDecimal insteadof Double , if you want to eliminate the E .


Changing the data structure because you don't like how it is automatically formatted when you print it -- without formatting -- is a bit silly, isn't it? Would it not be better to format it? Maybe either use the printf() method (java.io.PrintStream) or the java.text.DecimalFormat class?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

David is right. The E8 means "times 10 to the power 8", or 100,000,000. So 8.28124999171875E8 is actually 828124999.171875 - which is roughly the same as 828124999.17. That 0.001875 may be caused by rounding issues, something double and float are notorious for - not just in Java but in just about any programming language.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Sahil Sharma wrote:Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.


When formatting at the end, wonder what kind of rounding (RoundingMode) OP used or others suggest to use here. Because using different rounding methods and scale, together, would return different results, anyways. What I meant to say is that this calculation might have matched the result from using double data types and formatting at the end with a specific rounding might not match for another calculation. In complex and lengthier or looped calculations, the errors just propagate.

Whereas BigDecimal pretty much matches the results with those from the calculator. Here is an example quoted from the web, that can be solved by both BigDecimal and formatting.



The above problem can be solved by using BigDecimal as follows:



But because calculations are a pain and performance worse you could always do something like this:

which will also give the result same as the BigDecimal.

Not only is the choice of data type tricky but choice of roundingMode and scale are also tricky to just get calculator kind calculations. Could not come across any recently written articles when I googled about the best practices in using BigDecimal, when to use and how to use for optimal performance, any suggestions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrong value