0

I am attempting to create a calculator app, when I add three doubles together ex. (1.1 + 1.1 + 1.1) 3.3000000000000003 is returned. This only occurs when three numbers are added together (1.1 + 1.1) returns 2.2

Is their a way to use decimal format to round only if 2 digits in a row are zero to prevent this from happening?

Thank you!

Please let me know if you need to see more of my code, I think this is sufficient though I may be wrong.

equals button method

equals_button_IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new value being added everytime addition button is pressed, last number to be added will not have addition button
                //pressed after it, so this adds the last value to the values array list
                values.add(current_screen_value);
                System.out.println(values);
                double sum = 0;
                for(int i = 0; i<values.size(); i++){
                    sum+= Double.parseDouble(values.get(i));
                }
                System.out.println(sum);
                clearScreen(null);
                updateScreen(String.valueOf(sum));


            }
        });
Lontronix
  • 193
  • 2
  • 15
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) I'm suggesting this as a duplicate because it seems likely you need to start solving this problem by learning about floating point approximations. – O. Jones May 28 '17 at 18:17
  • As to why this is happening you might want to have a look at this post: [Floating point inaccuracy examples](https://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples) – vega8 May 28 '17 at 18:22
  • @O.Jones The question is similar to mine though the answers only provide an explanation, not a solution – Lontronix May 28 '17 at 18:26
  • @vega8 I am looking for a solution, not just an explanation I updated my question to reflect that. I am looking for a way to round in specific situations like when 2 digits in a row are 0. – Lontronix May 28 '17 at 18:30
  • Explanations allow you to create your own solutions, @Lontronix, A good one is https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html. Another good one for your solution is the normal Java documentation: http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html and related, http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.util.Locale-java.lang.String-java.lang.Object...- and overload, http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html It's always good to Read the Fine Manual. – Lew Bloch May 28 '17 at 21:17
  • Excuse me, but `System.out.println` in a GUI? – Lew Bloch May 28 '17 at 21:19

1 Answers1

1

This is happening probably because of the encoding of Double numbers. To solve this you can use BigDecimal class (as here)

bennes
  • 132
  • 2
  • 7
  • The numbers I am adding together are not very large (1.1 and 2.2) Can you explain why this would be necessary? – Lontronix May 28 '17 at 18:28
  • Float and double numbers are stored in a particular way. You can discover more [here](https://en.wikipedia.org/wiki/IEEE_floating_point) Please note that BigDecimal is used not only for **big** numbers as we can think, but for **precised** numbers – bennes May 28 '17 at 18:40
  • Sigh. If computers were motorcycles, the licensing exam would have a couple of questions about how floating point numbers work. They are approximations. – O. Jones May 28 '17 at 19:27
  • 1
    `BigDecimal` is a sledge hammer to pound a nail here. Perhaps it would be enough simply to format the string representation as desired rather than perform the calculations in perfect precision. Only the OP knows for sure. – Lew Bloch May 28 '17 at 21:19