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 BigDecimalDouble.MAX_VALUEconverted to aBigDecimal.static final BigDecimalLong.MAX_VALUEconverted to aBigDecimal.static final BigIntegerLong.MAX_VALUEconverted to aBigInteger.static final BigDecimalDouble.MIN_VALUEconverted to aBigDecimal.static final BigDecimalLong.MIN_VALUEconverted to aBigDecimal.static final BigIntegerLong.MIN_VALUEconverted 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>
booleanReturnstrueif 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 aNumberof the specified type.static booleanReturnstrueif the specified string can be parsed into aBigDecimal.static booleanReturnstrueif the specified string can be parsed into aBigIntegerwithout loss of information.static booleanReturnstrueif the specified string can be parsed into abytewithout loss of information.static booleanReturnstrueif the specified string can be parsed into adoublewithout loss of information.static booleanReturnstrueif the specified string can be parsed into afloatwithout loss of information.static booleanReturnstrueif the specified string can be parsed into anintwithout loss of information.static booleanisIntegral(Class<?> type) Returnstrueif the specified class is one ofByte,Short,Integer,Long,BigInteger.static booleanisIntegral(Number number) Returnstrueif the specified number is an integral number.static booleanReturnstrueif the specified string can be parsed into alongwithout loss of information.static booleanisRound(double d) Determines whether the specified double's fractional part is zero or absent.static booleanisRound(float f) Determines whether the specified float's fractional part is zero or absent.static booleanisRound(BigDecimal bd) Determines whether the specified BigDecimal's fractional part is zero or absent.static booleanReturnstrueif the specified string can be parsed into ashortwithout loss of information.static booleanReturnstrueif the specified class is one of the standard primitive number wrappers:Byte,Short,Integer,Long,Float,Double.static booleanReturnstrueif 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 BigDecimalParses the specified string into aBigInteger.static BigIntegerParses the specified string into aBigInteger.static byteParses the specified string into abyte.static doubleParses the specified string into adouble.static floatparseFloat(String s) Parses the specified string into afloat.static intParses the specified string into aint.static longParses the specified string into along.static shortparseShort(String s) Parses the specified string into ashort.static BigDecimalConverts aNumberof an unspecified type to aBigDecimal.static Optional<BigDecimal> Returns an emptyOptionalif the specified string cannot be parsed into BigDecimal, else anOptionalcontaining theBigDecimalvalue parsed out of the string.static Optional<BigInteger> Returns an emptyOptionalif the specified string cannot be parsed into BigInteger, else anOptionalcontaining theBigIntegervalue parsed out of the string.static OptionalIntReturns an emptyOptionalIntif the specified string cannot be parsed into an 8-bit integer, else anOptionalIntcontaining thebytevalue parsed out of the string.static OptionalDoubleReturns an emptyOptionalDoubleif the specified string cannot be parsed into adoublevalue, else anOptionalDoublecontaining thedoublevalue parsed out of the string.static OptionalDoubleReturns an emptyOptionalDoubleif the specified string cannot be parsed into a regular, finitefloatvalue, else anOptionalDoublecontaining thefloatvalue parsed out of the string.static OptionalIntReturns an emptyOptionalIntif the specified string cannot be parsed into a 32-bit integer, else anOptionalIntcontaining theintvalue parsed out of the string.static OptionalLongReturns an emptyOptionalLongif the specified string cannot be parsed into a 64-bit integer, else anOptionalLongcontaining thelongvalue parsed out of the string.static OptionalIntReturns an emptyOptionalIntif the specified string cannot be parsed into a 16-bit integer, else anOptionalIntcontaining theshortvalue parsed out of the string.
-
Field Details
-
MIN_DOUBLE_BD
Double.MIN_VALUEconverted to aBigDecimal. -
MAX_DOUBLE_BD
Double.MAX_VALUEconverted to aBigDecimal. -
MIN_LONG_BD
Long.MIN_VALUEconverted to aBigDecimal. -
MAX_LONG_BD
Long.MAX_VALUEconverted to aBigDecimal. -
MIN_LONG_BI
Long.MIN_VALUEconverted to aBigInteger. -
MAX_LONG_BI
Long.MAX_VALUEconverted to aBigInteger.
-
-
Method Details
-
isWrapper
Returnstrueif 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
Returnstrueif 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
Returnstrueif 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
Returnstrueif 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 anintwithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
intvalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isInt
Returnstrueif the specified string can be parsed into anintwithout 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
intwithout causing integer overflow
-
toInt
Returns an emptyOptionalIntif the specified string cannot be parsed into a 32-bit integer, else anOptionalIntcontaining theintvalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalIntcontaining theintvalue 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 alongwithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
longvalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isLong
Returnstrueif the specified string can be parsed into alongwithout 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
longwithout causing integer overflow
-
toLong
Returns an emptyOptionalLongif the specified string cannot be parsed into a 64-bit integer, else anOptionalLongcontaining thelongvalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalLongcontaining thelongvalue 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 adoublewithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
doublevalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isDouble
Returnstrueif the specified string can be parsed into adoublewithout 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 emptyOptionalDoubleif the specified string cannot be parsed into adoublevalue, else anOptionalDoublecontaining thedoublevalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalDoublecontaining thedoublevalue 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 afloatwithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
floatvalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isFloat
Returnstrueif the specified string can be parsed into afloatwithout 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 emptyOptionalDoubleif the specified string cannot be parsed into a regular, finitefloatvalue, else anOptionalDoublecontaining thefloatvalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalDoublecontaining thefloatvalue 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 ashortwithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
shortvalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isShort
Returnstrueif the specified string can be parsed into ashortwithout 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
shortwithout causing integer overflow
-
toShort
Returns an emptyOptionalIntif the specified string cannot be parsed into a 16-bit integer, else anOptionalIntcontaining theshortvalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalIntcontaining theshortvalue 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 abytewithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
bytevalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isByte
Returnstrueif the specified string can be parsed into abytewithout 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
bytewithout causing an integer overflow
-
toByte
Returns an emptyOptionalIntif the specified string cannot be parsed into an 8-bit integer, else anOptionalIntcontaining thebytevalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
OptionalIntcontaining thebytevalue 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 aBigIntegerwithout loss of information, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
BigIntegervalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number, or if conversion would lead to loss of information
-
isBigInteger
Returnstrueif the specified string can be parsed into aBigIntegerwithout 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 emptyOptionalif the specified string cannot be parsed into BigInteger, else anOptionalcontaining theBigIntegervalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
Optionalcontaining theBigIntegervalue parsed out of the string
-
parseBigDecimal
Parses the specified string into aBigInteger. If the string does not represent a number, aTypeConversionExceptionis thrown.- Parameters:
s- the string to be parsed- Returns:
- the
BigDecimalvalue represented by the string - Throws:
TypeConversionException- if the string does not represent or number
-
isBigDecimal
Returnstrueif 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 emptyOptionalif the specified string cannot be parsed into BigDecimal, else anOptionalcontaining theBigDecimalvalue parsed out of the string.- Parameters:
s- the string to be parsed- Returns:
- an
Optionalcontaining theBigDecimalvalue 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 anTypeConversionExceptionif the string is not a number, or if the number is too big to fit into the target type.- Type Parameters:
T- the type ofNumberto convert the string to- Parameters:
s- the string to be parsedtargetType- the class of theNumbertype- Returns:
- a
Numberof 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 aNumberof the specified type.- Type Parameters:
T- the type ofNumberto convert the string to- Parameters:
s- the string to be parsedtargetType- the class of theNumbertype- Returns:
- whether the specified string can be parsed into a
Numberof the specified type
-
toBigDecimal
Converts aNumberof an unspecified type to aBigDecimal.- Parameters:
n- the number- Returns:
- the
BigDecimalrepresenting 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 aTypeConversionExceptionif the number cannot be converted to the target type without loss of information. The number is allowed to benull, in which casenullwill 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
Returnstrueif the specified number can be converted to the specified target type without loss of information. The number is allowed to benull, in which casetruewill be returned.- Type Parameters:
T- the type ofNumberto convert to- Parameters:
number- theNumberto converttargetType- the type ofNumberto 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- thefloatto 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- thedoubleto 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- theBigDecimalto inspect- Returns:
- whether the specified BigDecimal's fractional part is zero or absent
-