Package com.globalmentor.java
Class Numbers
- java.lang.Object
-
- com.globalmentor.java.Numbers
-
public class Numbers extends java.lang.Object
Utilities for manipulating numbers.- Author:
- Garret Wilson
-
-
Field Summary
Fields Modifier and Type Field Description static double
ONE_THIRD_DOUBLE
The double value representing 1/3.static double
TWO_THIRDS_DOUBLE
The double value representing 2/3.
-
Constructor Summary
Constructors Constructor Description Numbers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
asDecimalValue(java.lang.Number number)
Returns the value of the given number if the number is decimal.static long
asIntegralValue(java.lang.Number number)
Returns the value of the given number if the number is integral.static int
compare(java.lang.Number number1, java.lang.Number number2)
Compares the two specified numbers.static boolean
isDecimal(java.lang.Number number)
Determines if the given number is a non-integer decimal type:Float
,Double
, orBigDecimal
.static boolean
isIntegral(java.lang.Number number)
Determines if the given number is an integral type:BigInteger
,Byte
,Integer
,Long
, orShort
.static int
sort(java.lang.Object object1, java.lang.Object object2)
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
.static int
sort(java.lang.Object object1, java.lang.Object object2, boolean ambiguous)
Compares the two specified objects, one of which must be an instance ofNumber
, and either of which may benull
.static java.math.BigDecimal
toBigDecimal(java.lang.Number number)
Returns a big decimal representation of the given number.
-
-
-
Field Detail
-
ONE_THIRD_DOUBLE
public static final double ONE_THIRD_DOUBLE
The double value representing 1/3.- See Also:
- Constant Field Values
-
TWO_THIRDS_DOUBLE
public static final double TWO_THIRDS_DOUBLE
The double value representing 2/3.- See Also:
- Constant Field Values
-
-
Method Detail
-
asIntegralValue
public static long asIntegralValue(java.lang.Number number)
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:
isIntegral(Number)
-
asDecimalValue
public static double asDecimalValue(java.lang.Number number)
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:
isDecimal(Number)
-
isIntegral
public static boolean isIntegral(java.lang.Number number)
Determines if the given number is an integral type:BigInteger
,Byte
,Integer
,Long
, orShort
.- Parameters:
number
- The number to examine.- Returns:
true
if the given number is an integral type.
-
isDecimal
public static boolean isDecimal(java.lang.Number number)
Determines if the given number is a non-integer decimal type:Float
,Double
, orBigDecimal
.- Parameters:
number
- The number to examine.- Returns:
true
if the given number is a non-integer decimal type.
-
toBigDecimal
public static java.math.BigDecimal toBigDecimal(java.lang.Number number)
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
public static int compare(java.lang.Number number1, java.lang.Number number2)
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:
java.lang.NullPointerException
- if either of the given numbers isnull
.
-
sort
public static int sort(java.lang.Object object1, java.lang.Object object2)
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:
java.lang.IllegalArgumentException
- if neither of the given objects is an instance ofNumber
.
-
sort
public static int sort(java.lang.Object object1, java.lang.Object object2, boolean ambiguous)
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:
java.lang.IllegalArgumentException
- if neither of the given objects is an instance ofNumber
and non-ambiguous sorting is requested.
-
-