You can create a BigDecimal out of an integer using the BigDecimal(int)
constructor, as in new BigDecimal(1)
or BigDecimal.ONE
(thanks, Herp Derpington).
Like Integer and Double, `BigDecimal instances are immutable, which means that any one BigDecimal instance will never change value, and thus you can share or reuse instances that represent the same number.
Unlike Integer and Double, BigDecimal does not participate in autoboxing, which is the translation from int
primitives to Integer
object instances (and likewise across other types). In Java, operators like +
and /
apply only to primitives; though you can call sqf.add(BigDecimal.ONE)
, there is no way to use +
or call sqf.add(1)
.
To operate on BigDecimal instances, you'll need to constrain yourself to its methods (such as add
and divide
), passing in BigDecimal instances (newly-created, if necessary). divide
, in particular, offers a number of method overloads that allow you to choose the way that the number is rounded or scaled—which is one of its advantages over the built-in floating-point division; if you encounter trouble, you may want to specify ROUND_TO_EVEN
, which best matches the IEEE 754 behavior in the Java Language spec.