• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrong value

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use BigDecimal insteadof Double , if you want to eliminate the E .
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are the same value (more or less).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic