This question already has an answer here:

```
public double hypotenuse()
{
//return Math.hypot(leg, leg); //returns 7.0710678118654755
//return 0.1 * Math.floor(Math.hypot(leg, leg) * 10); //returns 7.0
//return 0.1 * Math.ceil(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
return 0.1 * Math.round(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
}
```

I am trying to round to the nearest 10th place, so this number should round up to 7.1. Why does this method work with Math.floor but not with Math.round? Does anyone have any insight?

Thanks,

Sky

# Best How To :

In regards to your answer to yourself, you don't want to convert an object of type A to type B, operate on it, and then convert it back to type A. Not if you can do anything else.

The following does what you want it to do:

```
public double hypotenuse()
{
return Math.round(Math.hypot(leg, leg) * 10) / 10.0;
}
```

I'm not entirely sure why. It would take some digging into what this compiles down to at the assembly level.

The *real problem* is that each double in Java only has a limited number of bits to store its decimal portion. There are an infinite number of possible real numbers in real life. There is no way you can represent every single real number with only 64 bits. This is why you should avoid floating point calculations in banking apps and software that requires the number to be exact. Multiplying a few doubles together can introduce quite a significant error margin in the calculation. See this explanation on another SO answer, which does a great job of explaining this further.

If you really need it to be exact, use `BigDecimal`

.