@Status(stage=PRODUCTION, unitTests=COMPLETE) @Review(by="Kees Schotanus", at="2009-09-28") @ThreadSafety(level=IMMUTABLE) public final class CDouble extends Number implements Comparable<CDouble>
Double
is that
this class has (limited) support for arithmetic operations that protect
against both underflow and overflow.Double
is final so this class extends
class Number
instead. This class is immutable and contains private
constructors in combination with static factory methods as suggested by
Joshua Bloch (Effective Java, Second Edition, item 15).Modifier and Type | Field and Description |
---|---|
static double |
FOUR
The constant value 4.0.
|
static double |
HUNDRED_PERCENT
100%.
|
static double |
ONE_FOURTH
1/4.
|
static double |
ONE_THIRD
1/3.
|
static double |
TEN_PERCENT
10%.
|
static double |
THREE
The constant value 3.0.
|
static double |
THREE_QUARTERS
3/4.
|
static double |
TWO_THIRDS
2/3.
|
Modifier and Type | Method and Description |
---|---|
CDouble |
add(double value)
Adds the supplied value to this value.
|
static double |
add(double x,
double y)
Adds y to x.
|
byte |
byteValue()
Returns the value of this CDouble as a byte.
|
int |
compareTo(CDouble object)
Compares two CDouble objects numerically.
|
CDouble |
divide(double value)
Divides this value by the supplied value.
|
static double |
divide(double x,
double y)
Divides x by y.
|
double |
doubleValue()
Returns the value of this CDouble as a double.
|
static boolean |
equals(double left,
double right,
double delta)
Determines whether the supplied left and right double are equal to
each other within the limit of the supplied delta.
|
boolean |
equals(Object object)
Determines whether this object and the supplied object are the same.
|
float |
floatValue()
Returns the value of this CDouble as a float.
|
static int |
getFractionDigits(double value,
int roundTo)
Calculates the number of digits after the decimal point of the supplied
value.
|
int |
getFractionDigits(int roundTo)
Calculates the number of digits after the decimal point.
|
static int |
getIntegerDigits(double value,
int roundTo)
Calculates the number of digits before the decimal point.
|
int |
getIntegerDigits(int roundTo)
Calculates the number of digits before the decimal point.
|
int |
hashCode()
Computes the hash code value for this object.
|
int |
intValue()
Returns the value of this CDouble as an int.
|
long |
longValue()
Returns the value of this CDouble as a long.
|
CDouble |
multiply(double value)
Multiplies this value by the supplied value.
|
static double |
multiply(double x,
double y)
Multiplies x by y.
|
static double |
round(double value,
int decimals)
Rounds the supplied value to the supplied number of decimals.
|
CDouble |
round(int decimals)
Rounds this value to the supplied number of decimals.
|
static double |
roundUp(double value,
int decimals)
Rounds the supplied value up, to the supplied number of decimals.
|
short |
shortValue()
Returns the value of this CDouble as a short.
|
static double |
subtract(double x,
double y)
Subtracts y from x.
|
CDouble |
subtract(long value)
Subtracts the supplied value from this value.
|
String |
toString()
Converts the stored primitive double value to a String.
|
static CDouble |
valueOf(int value)
Returns a CDouble object holding the value of the supplied value.
|
static CDouble |
valueOf(long value)
Returns a CDouble object holding the value of the supplied value.
|
static CDouble |
valueOf(Number value)
Returns a CDouble object holding the value of the supplied value.
|
static CDouble |
valueOf(String value)
Returns a CDouble object holding the parsed value of the supplied value.
|
public static final double THREE
public static final double FOUR
public static final double ONE_THIRD
public static final double TWO_THIRDS
public static final double ONE_FOURTH
public static final double THREE_QUARTERS
public static final double TEN_PERCENT
public static final double HUNDRED_PERCENT
public byte byteValue()
public short shortValue()
shortValue
in class Number
public int intValue()
public long longValue()
public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public static CDouble valueOf(int value)
value
- The int value.public static CDouble valueOf(long value)
value
- The long value.IllegalArgumentException
- When the supplied value could not be
converted to a double without losing precision.public static CDouble valueOf(Number value)
value
- The Number that holds the value.NullPointerException
- When the supplied value is null.public static CDouble valueOf(String value)
value
- The String that is parsed.NumberFormatException
- When the supplied value does not contain a
parsable double (this includes a null or empty value).Double.parseDouble(String)
public boolean equals(Object object)
Double.equals(Object)
.public static boolean equals(double left, double right, double delta)
left
- The double on the left of the mathematical equals sign.right
- The double on the right of the mathematical equals sign.delta
- The limit within the two doubles are considered to be equal.public int hashCode()
CLong
is used to compute this hash code.public int compareTo(CDouble object)
compareTo
in interface Comparable<CDouble>
object
- CDouble object to compare with this object.NullPointerException
- When the supplied object is null.Comparable
,
Double.compare(double, double)
public CDouble add(double value)
value
- Value to add to the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.add(double, double)
public static double add(double x, double y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.public CDouble subtract(long value)
value
- Value to subtract from the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.subtract(double, double)
public static double subtract(double x, double y)
x
- Operand one.y
- operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.public CDouble multiply(double value)
value
- Value to multiply by the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.multiply(double, double)
public static double multiply(double x, double y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs, possibly due to one of the values being infinite.public CDouble divide(double value)
value
- Value by which this value will be divided.ArithmeticException
- When an arithmetic overflow occurs or when
this value is infinite or when the supplied value is 0.divide(double, double)
public static double divide(double x, double y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic overflow occurs or when
the supplied x value is infinite or when the supplied value is 0.public CDouble round(int decimals)
round(double, int)
for details.decimals
- The number of decimal places to which this value should be rounded.round(double, int)
public static double round(double value, int decimals)
Value | Decimals | Result |
---|---|---|
12.5 | 0 | 13 Equidistant, round up |
-12.5 | 0 | -12 Equidistant, -12 > -13 |
17.457 | 2 | 17.46 |
17.457 | 1 | 17.5 |
17.457 | 0 | 17 |
17.457 | -1 | 20 Rounds to tens |
value
- The value to round.decimals
- The number of decimal places to which this value should
be rounded.
public static double roundUp(double value, int decimals)
round(double, int)
.value
- The value to round up.decimals
- The number of decimal places to which this value should be rounded.
public int getIntegerDigits(int roundTo)
roundTo
- Number of decimal places to round to.
IllegalArgumentException
- When the supplied roundTo value is
negative.getIntegerDigits(double, int)
public static int getIntegerDigits(double value, int roundTo)
value
- Value for which the number of integer digits is calculated.roundTo
- Number of decimal places to round to.
Value | Round to | Result |
---|---|---|
0.3333 | 2 | 0 |
99.95 | 1 | 3 |
99.95 | 2 | 2 |
IllegalArgumentException
- When the supplied roundTo value is
negative.public int getFractionDigits(int roundTo)
roundTo
- Number of decimal places to round to.
IllegalArgumentException
- When the supplied roundTo value is
negative.getFractionDigits(double, int)
public static int getFractionDigits(double value, int roundTo)
value
- The value to get the number of fraction digits of.roundTo
- Number of decimal places to round to.
IllegalArgumentException
- When the supplied roundTo value is
negative.Copyright © 2008–2018. All rights reserved.