0
 public void Calculate(double value){
        int quarter, dime, nickel, remainder;
        value *= 100;
        System.out.println(value);

This is part of a simple coin counter program (I'm learning programming)--I have value set to 2.26 but found that I didn't get the desired answer, so I did System.out.println(value) to pinpoint where I am not getting the value expected. With value = 2.26, I should get 226 but instead I get 225.99999999999997, which throws off the entire program.

Iteration
  • 63
  • 4
  • 1
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Vincent van der Weele Jan 13 '15 at 11:33
  • This answer may help you understand the "issue" http://stackoverflow.com/a/4937591/1379371 – ferjani Jan 13 '15 at 11:37
  • Would you say BigDecimal is the best way to deal with all rounding errors like the one I'm encountering? I don't want to use a solution that applies to an insignificant number of cases because I want to be able to rely on one solution to the same kind of problem every time I encounter it. – Iteration Jan 13 '15 at 11:54

3 Answers3

0

You are using double. Double stores many places after decimal in memory so 2.26 is an approximation of a value which is like 2.25999999...

You must round your results to two places of decimal for accurate value.

If you want to know how to round numbers refer to this answer enter link description here

Community
  • 1
  • 1
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
  • When I simply print the "value" inside the function,it gives 2.26. Why does not it give value like 2.259999 in this case? why it uses approximation only if I change the value by multiplying it by another number,say 10 or 100? – hermit Jan 13 '15 at 11:45
  • 2
    @pramithasdhakal `double a = 2.26;` means let `a` be the closest `double` to 2.26. When you print `a` you see 2.26 because there are no `double` values closer to to the decimal value 2.26. However the true value of `a` is actually less than 2.26, so when you multiply by 100 the answer is less than 226. Unlike 2.26, 226 can be represented as a double. So printing `a * 100` doesn't print `226` because the output `226` is reserved for printing the "real" 226. – Paul Boddington Jan 13 '15 at 12:02
  • Thanks... that was an pretty interesting explanation.. – hermit Jan 13 '15 at 12:20
0

You can add casting to float, then it works fine

public static void main(String[] args) {
    Calculate ((float) 2.26);       
}

public static void Calculate(float value){
    int quarter, dime, nickel, remainder;
    value *= 100;
    System.out.println(value);
}
Pieczarr
  • 348
  • 4
  • 13
  • 1
    Please read through this question- http://stackoverflow.com/questions/16627813/why-is-comparing-a-float-to-a-double-inconsistent-in-java – DhruvJoshi Jan 13 '15 at 11:42
0

Here is the solution

 public void Calculate(double value){
        int quarter, dime, nickel, remainder;
        value *= 100;
        System.out.println(round(value,0));
 }
Junaid
  • 2,572
  • 6
  • 41
  • 77