public class NumberUtil extends Object
Constructor and Description |
---|
NumberUtil() |
Modifier and Type | Method and Description |
---|---|
static Number |
coerceNumber(Number number)
Tests whether a given number can be safely coerced to Double or
Integer without changing the value of the number.
|
static Number |
commonPrecisionNumber(Number number,
Number referenceNumber)
Coerces the given number to a common or greater precision than referenceNumber,
BigDecimal is greater than Double or
BigInteger which is greater than Long precision.
|
static Number |
higherPrecisionNumber(Number number)
Coerces the given number to Double or Long precision,
if possible.
|
public static Number coerceNumber(Number number)
Tests whether a given number can be safely coerced to Double or Integer without changing the value of the number. Safe means that coercing a number to Double or Integer and then coercing it back to the original type will result in the same value (no loss of precision). This is trivial for types, which have a smaller domain then Integer or Double, but for example a Long number may not be safely coerced to Integer.
If the coercion is safe, the number will be returned as either Double or Integer, as appropriate to the original precision. Otherwise the original number is returned.
number
- a number to coerce to Integer or Doublepublic static Number commonPrecisionNumber(Number number, Number referenceNumber)
Coerces the given number to a common or greater precision than referenceNumber, BigDecimal is greater than Double or BigInteger which is greater than Long precision.
number
- a number to coerce to a common precisionreferenceNumber
- another number to share the common precisionpublic static Number higherPrecisionNumber(Number number)
Coerces the given number to Double or Long precision, if possible. Note that this is only impossible for BigDecimal or BigInteger values, respectively, that are out of range of their primitive counterparts.
number
- a number to coerce to Long or DoubleCopyright © 2005, 2018 IBM Corporation and others. All Rights Reserved.