Modulus with doubles in Java
How do you deal with Java's weird behaviour with the modulus operator when using doubles? For example, you would expect the result of 3.9 - (3.9 % 0.1) to be 3.9 (and indeed, Google says I'm not going crazy), but when I run it in Java I get 3.8000000000000003 . I understand this is a result of how Java stores and processes doubles, but is there a way to work around it? Use a precise type if you need a precise result: double val = 3.9 - (3.9 % 0.1); System.out.println(val); // 3.8000000000000003 BigDecimal x = new BigDecimal( "3.9" ); BigDecimal bdVal = x.subtract( x.remainder( new BigDecimal(