Round a floating-point number down to the nearest integer?

int(x) Conversion to integer will truncate (towards 0.0), like math.trunc. For non-negative numbers, this is downward. If your number can be negative, this will round the magnitude downward, unlike math.floor which rounds towards -Infinity, making a lower value. (Less positive or more negative). Python integers are arbitrary precision, so even very large floats can be … Read more

round BigDecimal to nearest 5 cents

Using BigDecimal without any doubles (improved on the answer from marcolopes): public static BigDecimal round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode) { if (increment.signum() == 0) { // 0 increment does not make much sense, but prevent division by 0 return value; } else { BigDecimal divided = value.divide(increment, 0, roundingMode); BigDecimal result = divided.multiply(increment); return … Read more

Is inconsistency in rounding between Java 7 and Java 8 a bug?

It looks like this was a long-standing bug in JDK 7 that was finally fixed. See for example: https://bugs.openjdk.java.net/browse/JDK-8029896 https://bugs.openjdk.java.net/browse/JDK-7131459 There is a draft plan to provide the following advisory with JDK 8 which explains the issue: ——————————————————————— Area: Core Libraries / java.text Synopsis: A wrong rounding behavior of JDK7 has been fixed. The rounding … Read more