Class NumberMethods
java.lang.Object
org.klojang.convert.NumberMethods
Methods for parsing, inspecting and converting
Number
instances.
NB For mathematical operations, see MathMethods
.
- Author:
- Ayco Holleman
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigDecimal
Double.MAX_VALUE
converted to aBigDecimal
.static final BigDecimal
Long.MAX_VALUE
converted to aBigDecimal
.static final BigInteger
Long.MAX_VALUE
converted to aBigInteger
.static final BigDecimal
Double.MIN_VALUE
converted to aBigDecimal
.static final BigDecimal
Long.MIN_VALUE
converted to aBigDecimal
.static final BigInteger
Long.MIN_VALUE
converted to aBigInteger
. -
Method Summary
Modifier and TypeMethodDescriptionSafely converts a number of an unspecified type to a number of a definite type.static <T extends Number>
booleanReturnstrue
if the specified number can be converted to the specified target type without loss of information.static <T extends Number>
booleanTests whether the specified string can be parsed into aNumber
of the specified type.static boolean
Returnstrue
if the specified string can be parsed into aBigDecimal
.static boolean
Returnstrue
if the specified string can be parsed into aBigInteger
without loss of information.static boolean
Returnstrue
if the specified string can be parsed into abyte
without loss of information.static boolean
Returnstrue
if the specified string can be parsed into adouble
without loss of information.static boolean
Returnstrue
if the specified string can be parsed into afloat
without loss of information.static boolean
Returnstrue
if the specified string can be parsed into anint
without loss of information.static boolean
isIntegral
(Class<?> type) Returnstrue
if the specified class is one ofByte
,Short
,Integer
,Long
,BigInteger
.static boolean
isIntegral
(Number number) Returnstrue
if the specified number is an integral number.static boolean
Returnstrue
if the specified string can be parsed into along
without loss of information.static boolean
isRound
(double d) Determines whether the specified double's fractional part is zero or absent.static boolean
isRound
(float f) Determines whether the specified float's fractional part is zero or absent.static boolean
isRound
(BigDecimal bd) Determines whether the specified BigDecimal's fractional part is zero or absent.static boolean
Returnstrue
if the specified string can be parsed into ashort
without loss of information.static boolean
Returnstrue
if the specified class is one of the standard primitive number wrappers:Byte
,Short
,Integer
,Long
,Float
,Double
.static boolean
Returnstrue
if the specified number belongs to one of the primitive number wrappers.static <T extends Number>
TParses the specified string into a number of the specified type.static BigDecimal
Parses the specified string into aBigInteger
.static BigInteger
Parses the specified string into aBigInteger
.static byte
Parses the specified string into abyte
.static double
Parses the specified string into adouble
.static float
parseFloat
(String s) Parses the specified string into afloat
.static int
Parses the specified string into aint
.static long
Parses the specified string into along
.static short
parseShort
(String s) Parses the specified string into ashort
.static BigDecimal
Converts aNumber
of an unspecified type to aBigDecimal
.static Optional
<BigDecimal> Returns an emptyOptional
if the specified string cannot be parsed into BigDecimal, else anOptional
containing theBigDecimal
value parsed out of the string.static Optional
<BigInteger> Returns an emptyOptional
if the specified string cannot be parsed into BigInteger, else anOptional
containing theBigInteger
value parsed out of the string.static OptionalInt
Returns an emptyOptionalInt
if the specified string cannot be parsed into an 8-bit integer, else anOptionalInt
containing thebyte
value parsed out of the string.static OptionalDouble
Returns an emptyOptionalDouble
if the specified string cannot be parsed into adouble
value, else anOptionalDouble
containing thedouble
value parsed out of the string.static OptionalDouble
Returns an emptyOptionalDouble
if the specified string cannot be parsed into a regular, finitefloat
value, else anOptionalDouble
containing thefloat
value parsed out of the string.static OptionalInt
Returns an emptyOptionalInt
if the specified string cannot be parsed into a 32-bit integer, else anOptionalInt
containing theint
value parsed out of the string.static OptionalLong
Returns an emptyOptionalLong
if the specified string cannot be parsed into a 64-bit integer, else anOptionalLong
containing thelong
value parsed out of the string.static OptionalInt
Returns an emptyOptionalInt
if the specified string cannot be parsed into a 16-bit integer, else anOptionalInt
containing theshort
value parsed out of the string.
-
Field Details
-
MIN_DOUBLE_BD
Double.MIN_VALUE
converted to aBigDecimal
. -
MAX_DOUBLE_BD
Double.MAX_VALUE
converted to aBigDecimal
. -
MIN_LONG_BD
Long.MIN_VALUE
converted to aBigDecimal
. -
MAX_LONG_BD
Long.MAX_VALUE
converted to aBigDecimal
. -
MIN_LONG_BI
Long.MIN_VALUE
converted to aBigInteger
. -
MAX_LONG_BI
Long.MAX_VALUE
converted to aBigInteger
.
-
-
Method Details
-
isWrapper
Returnstrue
if the specified class is one of the standard primitive number wrappers:Byte
,Short
,Integer
,Long
,Float
,Double
.- Parameters:
numberType
- the class to test- Returns:
- whether the class is a primitive number wrapper
- See Also:
-
isWrapper
Returnstrue
if the specified number belongs to one of the primitive number wrappers.- Parameters:
number
- the number to test- Returns:
- whether the specified number belongs to one of the primitive number wrappers
- See Also:
-
isIntegral
Returnstrue
if the specified class is one ofByte
,Short
,Integer
,Long
,BigInteger
.- Parameters:
type
- the class to test- Returns:
- whether the class is an integral number type
-
isIntegral
Returnstrue
if the specified number is an integral number.- Parameters:
number
- the number to test- Returns:
- whether the specified number is an integral number
- See Also:
-
parseInt
Parses the specified string into aint
. If the string does not represent a number, or if it cannot be parsed into anint
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
int
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isInt
Returnstrue
if the specified string can be parsed into anint
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether the specified string can be parsed into an
int
without causing integer overflow
-
toInt
Returns an emptyOptionalInt
if the specified string cannot be parsed into a 32-bit integer, else anOptionalInt
containing theint
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalInt
containing theint
value parsed out of the string
-
parseLong
Parses the specified string into along
. If the string does not represent a number, or if it cannot be parsed into along
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
long
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isLong
Returnstrue
if the specified string can be parsed into along
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether the specified string can be parsed into a
long
without causing integer overflow
-
toLong
Returns an emptyOptionalLong
if the specified string cannot be parsed into a 64-bit integer, else anOptionalLong
containing thelong
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalLong
containing thelong
value parsed out of the string
-
parseDouble
Parses the specified string into adouble
. If the string does not represent a number, or if it cannot be parsed into adouble
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
double
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isDouble
Returnstrue
if the specified string can be parsed into adouble
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a regular, finite
double
-
toDouble
Returns an emptyOptionalDouble
if the specified string cannot be parsed into adouble
value, else anOptionalDouble
containing thedouble
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalDouble
containing thedouble
value parsed out of the string
-
parseFloat
Parses the specified string into afloat
. If the string does not represent a number, or if it cannot be parsed into afloat
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
float
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isFloat
Returnstrue
if the specified string can be parsed into afloat
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a regular, finite
float
-
toFloat
Returns an emptyOptionalDouble
if the specified string cannot be parsed into a regular, finitefloat
value, else anOptionalDouble
containing thefloat
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalDouble
containing thefloat
value parsed out of the string
-
parseShort
Parses the specified string into ashort
. If the string does not represent a number, or if it cannot be parsed into ashort
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
short
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isShort
Returnstrue
if the specified string can be parsed into ashort
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a
short
without causing integer overflow
-
toShort
Returns an emptyOptionalInt
if the specified string cannot be parsed into a 16-bit integer, else anOptionalInt
containing theshort
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalInt
containing theshort
value parsed out of the string
-
parseByte
Parses the specified string into abyte
. If the string does not represent a number, or if it cannot be parsed into abyte
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
byte
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isByte
Returnstrue
if the specified string can be parsed into abyte
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a
byte
without causing an integer overflow
-
toByte
Returns an emptyOptionalInt
if the specified string cannot be parsed into an 8-bit integer, else anOptionalInt
containing thebyte
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
OptionalInt
containing thebyte
value parsed out of the string
-
parseBigInteger
Parses the specified string into aBigInteger
. If the string does not represent a number, or if it cannot be parsed into aBigInteger
without loss of information, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
BigInteger
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
isBigInteger
Returnstrue
if the specified string can be parsed into aBigInteger
without loss of information. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a
BigInteger
-
toBigInteger
Returns an emptyOptional
if the specified string cannot be parsed into BigInteger, else anOptional
containing theBigInteger
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
Optional
containing theBigInteger
value parsed out of the string
-
parseBigDecimal
Parses the specified string into aBigInteger
. If the string does not represent a number, aTypeConversionException
is thrown.- Parameters:
s
- the string to be parsed- Returns:
- the
BigDecimal
value represented by the string - Throws:
TypeConversionException
- if the string does not represent or number
-
isBigDecimal
Returnstrue
if the specified string can be parsed into aBigDecimal
. The argument is allowed to benull
, in which case the return value will befalse
.- Parameters:
s
- the string to be parsed- Returns:
- whether he specified string can be parsed into a
BigDecimal
-
toBigDecimal
Returns an emptyOptional
if the specified string cannot be parsed into BigDecimal, else anOptional
containing theBigDecimal
value parsed out of the string.- Parameters:
s
- the string to be parsed- Returns:
- an
Optional
containing theBigDecimal
value parsed out of the string
-
parse
public static <T extends Number> T parse(String s, Class<T> targetType) throws TypeConversionException Parses the specified string into a number of the specified type. Throws anTypeConversionException
if the string is not a number, or if the number is too big to fit into the target type.- Type Parameters:
T
- the type ofNumber
to convert the string to- Parameters:
s
- the string to be parsedtargetType
- the class of theNumber
type- Returns:
- a
Number
of the specified type - Throws:
TypeConversionException
- if the string does not represent or number, or if conversion would lead to loss of information
-
fitsInto
Tests whether the specified string can be parsed into aNumber
of the specified type.- Type Parameters:
T
- the type ofNumber
to convert the string to- Parameters:
s
- the string to be parsedtargetType
- the class of theNumber
type- Returns:
- whether the specified string can be parsed into a
Number
of the specified type
-
toBigDecimal
Converts aNumber
of an unspecified type to aBigDecimal
.- Parameters:
n
- the number- Returns:
- the
BigDecimal
representing the number
-
convert
public static <T extends Number, R extends Number> R convert(T number, Class<R> targetType) throws TypeConversionException Safely converts a number of an unspecified type to a number of a definite type. Throws aTypeConversionException
if the number cannot be converted to the target type without loss of information. The number is allowed to benull
, in which casenull
will be returned.- Type Parameters:
T
- the input typeR
- the output type- Parameters:
number
- the number to be convertedtargetType
- the class of the target type- Returns:
- an instance of the target type
- Throws:
TypeConversionException
-
fitsInto
Returnstrue
if the specified number can be converted to the specified target type without loss of information. The number is allowed to benull
, in which casetrue
will be returned.- Type Parameters:
T
- the type ofNumber
to convert to- Parameters:
number
- theNumber
to converttargetType
- the type ofNumber
to convert to- Returns:
- whether conversion will be lossless
-
isRound
public static boolean isRound(float f) Determines whether the specified float's fractional part is zero or absent.- Parameters:
f
- thefloat
to inspect- Returns:
- whether the specified float's fractional part is zero or absent
-
isRound
public static boolean isRound(double d) Determines whether the specified double's fractional part is zero or absent.- Parameters:
d
- thedouble
to inspect- Returns:
- whether the specified double's fractional part is zero or absent
-
isRound
Determines whether the specified BigDecimal's fractional part is zero or absent.- Parameters:
bd
- theBigDecimal
to inspect- Returns:
- whether the specified BigDecimal's fractional part is zero or absent
-