public class NumberUtil extends Object
Number
.Constructor and Description |
---|
NumberUtil() |
Modifier and Type | Method and Description |
---|---|
static <T extends Number> |
dynamicCast(Object obj,
Class<T> klass)
Attempts to convert the first argument (which must be a subclass of
Number ) into an instance of the second (which must be a
primitive wrapper type). |
static Number |
parse(String str,
Class<? extends Number> klass)
Parses the passed string into an instance of the specified class.
|
static String |
toHexString(long value,
int digits)
Returns a hexadecimal string representing the passed value, padded
as necessary to the specified number of digits.
|
public static Number parse(String str, Class<? extends Number> klass)
NumberFormatException
- if the passed string cannot be parsed
by the specified class.public static String toHexString(long value, int digits)
Integer.toHexString()
.
Example: toHex(17, 4)
returns "0011", while
toHex(-17, 4)
returns "FFEF".
Hex digits are uppercase; call toLowerCase()
on the
returned string if you don't like that.
The returned value can have as many digits as you like -- you're not limited to the 16 digits that a long can hold.
public static <T extends Number> T dynamicCast(Object obj, Class<T> klass)
Number
) into an instance of the second (which must be a
primitive wrapper type). If passed null
, will return
null
. If passed an instance of the desired type, will
return it unchanged.ClassCastException
- if the first argument is not a subclass
of Number
, or the second is not a supported type.