Im having a problem with an app i want to create. It's about counting money you spent. I made a button. every time i press it the number of article x is increased by one. Just to test the whole thing, i set the price for the article on 0,3€. Above and below the button the number of articles and the money spent on them is displayed. I click the button, the money gets set to 0.3. I press again, and it gets set to 0.6. When I then press the third time, it doesnt say 0.9 but 0.89999999? Why doesent it just go to 0.9? I already tried rounding the whole thing but it doesnt work or my app just crashes... Maybe the mistake is really stupid but im kinda new to programming with Android Studio/Java and at this point im kinda stuck right now. Heres the piece of code im talking about:
TextView numbertextview;
TextView moneytextview;
Button addbutton;
double price, money;
int number;
...
money = 0;
moneytextview.setText(Double.toString(money));
price = 0.3;
number = 0;
numbertextview.setText(Integer.toString(number));
addbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number++;
numbertextview.setText(Integer.toString(number));
money = money + price;
moneytextview.setText(Double.toString(money) + "0€");
}
});