Question:
How to test if a BigDecimal is less then zero?
How to compare BigDecimal to an int?
How to compare BigDecimal to an int?
Answer:
number.compareTo(BigDecimal.ZERO)
Code:
import java.math.*; public class BigDecimalTest { public static void main(String[] args) { BigDecimal bd = new BigDecimal(-1.5); if (bd.compareTo(BigDecimal.ZERO) < 0) { System.out.println(bd + " is less then zero"); } } }
Output:
$ java BigDecimalTest -1.5 is less then zero