Package com.globalmentor.java
Class Numbers
java.lang.Object
com.globalmentor.java.Numbers
Utilities for manipulating numbers.
- Author:
- Garret Wilson
-
Field Summary
Modifier and TypeFieldDescriptionstatic final double
The double value representing 1/3.static final double
The double value representing 2/3. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
asDecimalValue
(Number number) Returns the value of the given number if the number is decimal.static long
asIntegralValue
(Number number) Returns the value of the given number if the number is integral.static int
Compares the two specified numbers.static boolean
static boolean
isIntegral
(Number number) static int
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
.static int
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
.static BigDecimal
toBigDecimal
(Number number) Returns a big decimal representation of the given number.
-
Field Details
-
ONE_THIRD_DOUBLE
public static final double ONE_THIRD_DOUBLEThe double value representing 1/3.- See Also:
-
TWO_THIRDS_DOUBLE
public static final double TWO_THIRDS_DOUBLEThe double value representing 2/3.- See Also:
-
-
Constructor Details
-
Numbers
public Numbers()
-
-
Method Details
-
asIntegralValue
Returns the value of the given number if the number is integral.- Parameters:
number
- The number to examine.- Returns:
- The value of the given number if the number is integral, or -1 if the given number is not an integral.
- See Also:
-
asDecimalValue
Returns the value of the given number if the number is decimal.- Parameters:
number
- The number to examine.- Returns:
- The value of the given number if the number is decimal, or -1 if the given number is not decimal.
- See Also:
-
isIntegral
- Parameters:
number
- The number to examine.- Returns:
true
if the given number is an integral type.
-
isDecimal
- Parameters:
number
- The number to examine.- Returns:
true
if the given number is a non-integer decimal type.
-
toBigDecimal
Returns a big decimal representation of the given number. If the number is already aBigDecimal
, the same number object is returned.- Parameters:
number
- The number to return as a big decimal.- Returns:
- A big decimal representing the number.
-
compare
Compares the two specified numbers.- Parameters:
number1
- The first number to compare.number2
- The second number to compare.- Returns:
- The value
0
if the first number is numerically equal to the second number; a value less than0
if the first number is numerically less than the second number; or a value greater than0
if the first number is numerically greater than the second number. - Throws:
NullPointerException
- if either of the given numbers isnull
.
-
sort
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
. Numbers are sorted before non-numbers, and two numbers are compared.- Parameters:
object1
- The first object to compare.object2
- The second object to compare.- Returns:
- The value
0
if both are numbers and the first number is numerically equal to the second number; a value less than0
if only the first is a number, or both are numbers and the first number is numerically less than the second number; or a value greater than0
if only the second is a number, or both are numbers and the first number is numerically greater than the second number. - Throws:
IllegalArgumentException
- if neither of the given objects is an instance ofNumber
.
-
sort
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
. Numbers are sorted before non-numbers, and two numbers are compared. If ambiguous sorting is requested, if neither object is a number the objects will be considered equal; otherwise, if neither object is a number an exception will be thrown.- Parameters:
object1
- The first object to compare.object2
- The second object to compare.ambiguous
- Whether ambiguous sorting is allowed.- Returns:
- The value
0
if both are numbers and the first number is numerically equal to the second number; a value less than0
if only the first is a number, or both are numbers and the first number is numerically less than the second number; or a value greater than0
if only the second is a number, or both are numbers and the first number is numerically greater than the second number. - Throws:
IllegalArgumentException
- if neither of the given objects is an instance ofNumber
and non-ambiguous sorting is requested.
-