After a lot of messing around I came up with the following code to convert any Number subclass into a BigDecimal.
I'm not convinced however that this code is fully correct. I'm certainly not happy about how verbose it is!
Is there a better way of doing this, and are there any pitfalls to this approach of which I need to be aware (other than floating point imprecise representation issues of which I'm already aware)?
public DecimalSpec setValue (Number value) {
if (value instanceof Double) {
if ((((Double) value).isNaN ())
|| (((Double) value).isInfinite ())) {
throw new IllegalArgumentException ("Infinite or NaN values not allowed");
}
}
this.value = new BigDecimal (value.toString ());
return this;
}