0

I´m trying out eclipse for the first time and I´m trying to programm an Calculator with a numpad. Currently i´m programming the comma function but my code doesnt work.

Here is my code for the decimal places:

System.out.println(first+"");
System.out.println(dec_length(first));
first=((first*Math.pow(10, dec_length(first)))+1) / Math.pow(10, dec_length(first));
textfield.setText(""+format.format(first));

The method dec_length() is:

public static int dec_length(double input) {
    String param=(""+input);
    String vergleich =(".0");
    String sub= param.substring((param.indexOf('.')));
    if (sub.equals(vergleich)) {
        return 1;
    } else
        return sub.length();
}

When I try it it works for the first 2 decimal places but then it stops working.

Here is the console output:

5.0
1
5.1
2
5.109999999999999
16
5.109999999999999
16
5.109999999999999
16

Does anybody know whats wrong with this?

Sirion
  • 804
  • 1
  • 11
  • 33
  • 2
    Can you include what you actually would like the output to be? It's difficult to understand what you're trying to achieve. – mprivat Mar 29 '20 at 12:09
  • 1
    Also, please provide the code that you used to generate the output you reported and give a bit more context about where you placed the four line of code you used for "the decimal places". – Sirion Mar 29 '20 at 12:13
  • The code for the decimal places is inside a if statement of the button "1" of the calculator. The output is generated by the first two lines of the first code example. – Philipp Kraus Mar 29 '20 at 12:24
  • I would like the output to be 5.0-1 (5.0 is the current number and 1 is how many decimal places there are.) The 5.1099... should be 5.11 but that doesnt work. – Philipp Kraus Mar 29 '20 at 12:26
  • In general im trying to write the number 5.1111 by clicking the button "5" and then the button "," and then four times the button "1" but it stops working at 5.11 – Philipp Kraus Mar 29 '20 at 12:28
  • Here is the whole code if you want to see it but its not very pretty. https://pastebin.com/e9q9CXUT – Philipp Kraus Mar 29 '20 at 12:31
  • The dec_length() method is in line 59 and the rest is in line 106 – Philipp Kraus Mar 29 '20 at 12:33
  • I think your code is working, but the output is misleading - see, e.g., here: https://stackoverflow.com/questions/26557213/too-many-decimal-places-when-using-java#26557269 – Hans Mar 29 '20 at 12:42
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Ole V.V. Mar 29 '20 at 12:52
  • Welcome to Stack Overflow. I’m afraid that we need [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) including expected and observed output. – Ole V.V. Mar 29 '20 at 12:53

1 Answers1

2

The problem is that you assume that all decimal places can be represented exactly by the double type. But that is not the case.

I suggest you store the number as a string until you use it for calculation:

String first = "0";

And then either add a digit or a comma(you should of course make sure there is only one . in your number):

first += digit; // Add a digit from 0 to 9
first += '.'; // Call this when the comma function is called.

And afterwards, when you want to do the calculation part you can easily parse the string to a double:

double value = Double.parseDouble(first);
QuantumDeveloper
  • 737
  • 6
  • 15