Paji.R - 1 year ago 101

Java Question

I have 2 values which I'm trying to add together. Attempting so does not add the decimal place to the value.

`int pearInt = (int) Double.parseDouble(pear.getText());`

int appleInt = (int) Double.parseDouble(apple.getText());

double result = pearInt + appleInt;

total.setText("" + result);

`pear.getText()`

`apple.getText()`

`35.5`

`16.5`

`52.0`

`51.0`

Answer Source

The problem is your sum is of 2 ints:

```
int pearInt = (int) Double.parseDouble(pear.getText());
int appleInt = (int) Double.parseDouble(apple.getText());
```

This will produces 2 ints, because you are casting the doubles. So simply change to:

```
double pearInt = Double.parseDouble(pear.getText());
double appleInt = Double.parseDouble(apple.getText());
```

And you are good to go.