Package com.mckoi.util
Class BigNumber
java.lang.Object
java.lang.Number
com.mckoi.util.BigNumber
- All Implemented Interfaces:
Serializable
Extends BigDecimal to allow a number to be positive infinity, negative
infinity and not-a-number. This provides compatibility with float and
double types.
- Author:
- Tobias Downer
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns the big number as a BigDecimal object.byte
Returns this big number as a byte.boolean
Returns true if this BigNumber can be represented by a 32-bit int (has no scale).boolean
Returns true if this BigNumber can be represented by a 64-bit long (has no scale).int
Compares this BigNumber with the given BigNumber.double
Returns this big number as a double.boolean
The equals comparison uses the BigDecimal 'equals' method to compare values.float
Returns this big number as a float.static BigNumber
fromBigDecimal
(BigDecimal val) Creates a BigNumber from a BigDecimal.static BigNumber
fromData
(byte[] buf, int scale, byte state) Creates a BigNumber from the given data.static BigNumber
fromDouble
(double value) Creates a BigNumber from a double.static BigNumber
fromFloat
(float value) Creates a BigNumber from a float.static BigNumber
fromInt
(int value) Creates a BigNumber from an int.static BigNumber
fromLong
(long value) Creates a BigNumber from a long.static BigNumber
fromString
(String str) Creates a BigNumber from a string.int
getScale()
Returns the scale of this number, or -1 if the number has no scale (if it -inf, +inf or NaN).byte
getState()
Returns the state of this number.int
intValue()
Returns this big number as an int.long
Returns this big number as a long.setScale
(int d, int round_enum) short
Returns this big number as a short.int
signum()
sqrt()
byte[]
Returns this number as a byte array (unscaled).toString()
Returns this big number as a string.
-
Field Details
-
NEGATIVE_INFINITY
Statics for negative infinity, positive infinity and NaN. -
POSITIVE_INFINITY
-
NaN
-
BIG_NUMBER_ZERO
Statics for 0 and 1. -
BIG_NUMBER_ONE
-
-
Method Details
-
canBeRepresentedAsLong
public boolean canBeRepresentedAsLong()Returns true if this BigNumber can be represented by a 64-bit long (has no scale). -
canBeRepresentedAsInt
public boolean canBeRepresentedAsInt()Returns true if this BigNumber can be represented by a 32-bit int (has no scale). -
getScale
public int getScale()Returns the scale of this number, or -1 if the number has no scale (if it -inf, +inf or NaN). -
getState
public byte getState()Returns the state of this number. Returns either 1 which indicates negative infinity, 2 which indicates positive infinity, or 3 which indicates NaN. -
toByteArray
public byte[] toByteArray()Returns this number as a byte array (unscaled). -
toString
Returns this big number as a string. -
doubleValue
public double doubleValue()Returns this big number as a double.- Specified by:
doubleValue
in classNumber
-
floatValue
public float floatValue()Returns this big number as a float.- Specified by:
floatValue
in classNumber
-
longValue
public long longValue()Returns this big number as a long. -
intValue
public int intValue()Returns this big number as an int. -
shortValue
public short shortValue()Returns this big number as a short.- Overrides:
shortValue
in classNumber
-
byteValue
public byte byteValue()Returns this big number as a byte. -
asBigDecimal
Returns the big number as a BigDecimal object. Note that this throws an arith error if this number represents NaN, +Inf or -Inf. -
compareTo
Compares this BigNumber with the given BigNumber. Returns 0 if the values are equal, >0 if this is greater than the given value, and < 0 if this is less than the given value. -
equals
The equals comparison uses the BigDecimal 'equals' method to compare values. This means that '0' is NOT equal to '0.0' and '10.0' is NOT equal to '10.00'. Care should be taken when using this method. -
bitWiseOr
-
add
-
subtract
-
multiply
-
divide
-
abs
-
signum
public int signum() -
setScale
-
sqrt
-
fromDouble
Creates a BigNumber from a double. -
fromFloat
Creates a BigNumber from a float. -
fromLong
Creates a BigNumber from a long. -
fromInt
Creates a BigNumber from an int. -
fromString
Creates a BigNumber from a string. -
fromBigDecimal
Creates a BigNumber from a BigDecimal. -
fromData
Creates a BigNumber from the given data.
-