Sahil Sharma

Ranch Hand

Posts: 152

posted 7 years ago

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.

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.

posted 7 years ago

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

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

posted 7 years ago

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 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Sahil Sharma

Ranch Hand

Posts: 152

posted 7 years ago

Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.

Thanks guys for all the help.

Thanks guys for all the help.

Rama Krishna

Ranch Hand

Posts: 110

posted 7 years ago

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?

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?