1

I am trying to subtract two doubles from each other. I can't really change them to anything else as a lot of my project I am working on is already in doubles.

double numberOne = 0.7;
double numberTwo = 0.65;

return numberOne - numberTwo;

My unit test is throwing an error though. From the subtraction above I would expect to see 0.05 and or you know just .05.

Expected :0.05
Actual   :0.04999999999999993

I've been looking online for a solution and I just can't seem to find it. All help is welcome, thank you!

Doobie
  • 57
  • 2
  • 9
  • Try and write `0.05` as a binary value. `return BigDecimal.valueOf(numberOne).subtract(BigDecimal.valueOf(numberTwo)).doubleValue();` – Elliott Frisch Feb 24 '18 at 21:38
  • Related / duplicates: [How to round a number to n decimal places in Java](https://stackoverflow.com/q/153724) and [Retain precision with double in Java](https://stackoverflow.com/q/322749) – Bernhard Barker Feb 24 '18 at 21:42
  • Elliott Frisch thanks, alot that worked like a charm! – Doobie Feb 24 '18 at 21:45

0 Answers0