< prev index next >
src/java.base/share/classes/java/math/BigDecimal.java
Print this page
@@ -53,11 +53,11 @@
* otherwise, calculations can be carried out to a chosen precision
* and rounding mode by supplying an appropriate {@link MathContext}
* object to the operation. In either case, eight <em>rounding
* modes</em> are provided for the control of rounding. Using the
* integer fields in this class (such as {@link #ROUND_HALF_UP}) to
- * represent rounding mode is largely obsolete; the enumeration values
+ * represent rounding mode is deprecated; the enumeration values
* of the {@code RoundingMode} {@code enum}, (such as {@link
* RoundingMode#HALF_UP}) should be used instead.
*
* <p>When a {@code MathContext} object is supplied with a precision
* setting of 0 (for example, {@link MathContext#UNLIMITED}),
@@ -91,11 +91,11 @@
*
*
* <p>In general the rounding modes and precision setting determine
* how operations return results with a limited number of digits when
* the exact result has more digits (perhaps infinitely many in the
- * case of division) than the number of digits returned.
+ * case of division and square root) than the number of digits returned.
*
* First, the
* total number of digits to return is specified by the
* {@code MathContext}'s {@code precision} setting; this determines
* the result's <i>precision</i>. The digit count starts from the
@@ -194,22 +194,23 @@
* are interpreted similarly. Square brackets are used to represent
* the particular {@code BigInteger} and scale pair defining a
* {@code BigDecimal} value; for example [19, 2] is the
* {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
*
- * <p>Note: care should be exercised if {@code BigDecimal} objects
- * are used as keys in a {@link java.util.SortedMap SortedMap} or
- * elements in a {@link java.util.SortedSet SortedSet} since
- * {@code BigDecimal}'s <i>natural ordering</i> is <i>inconsistent
- * with equals</i>. See {@link Comparable}, {@link
- * java.util.SortedMap} or {@link java.util.SortedSet} for more
- * information.
*
* <p>All methods and constructors for this class throw
* {@code NullPointerException} when passed a {@code null} object
* reference for any input parameter.
*
+ * @apiNote Care should be exercised if {@code BigDecimal} objects
+ * are used as keys in a {@link java.util.SortedMap SortedMap} or
+ * elements in a {@link java.util.SortedSet SortedSet} since
+ * {@code BigDecimal}'s <i>natural ordering</i> is <em>inconsistent
+ * with equals</em>. See {@link Comparable}, {@link
+ * java.util.SortedMap} or {@link java.util.SortedSet} for more
+ * information.
+ *
* @see BigInteger
* @see MathContext
* @see RoundingMode
* @see java.util.SortedMap
* @see java.util.SortedSet
@@ -375,14 +376,14 @@
* Translates a character array representation of a
* {@code BigDecimal} into a {@code BigDecimal}, accepting the
* same sequence of characters as the {@link #BigDecimal(String)}
* constructor, while allowing a sub-array to be specified.
*
- * <p>Note that if the sequence of characters is already available
+ * @implNote If the sequence of characters is already available
* within a character array, using this constructor is faster than
* converting the {@code char} array to string and using the
- * {@code BigDecimal(String)} constructor .
+ * {@code BigDecimal(String)} constructor.
*
* @param in {@code char} array that is the source of characters.
* @param offset first character in the array to inspect.
* @param len number of characters to consider.
* @throws NumberFormatException if {@code in} is not a valid
@@ -399,18 +400,18 @@
* {@code BigDecimal} into a {@code BigDecimal}, accepting the
* same sequence of characters as the {@link #BigDecimal(String)}
* constructor, while allowing a sub-array to be specified and
* with rounding according to the context settings.
*
- * <p>Note that if the sequence of characters is already available
+ * @implNote If the sequence of characters is already available
* within a character array, using this constructor is faster than
* converting the {@code char} array to string and using the
* {@code BigDecimal(String)} constructor.
*
* @param in {@code char} array that is the source of characters.
* @param offset first character in the array to inspect.
- * @param len number of characters to consider..
+ * @param len number of characters to consider.
* @param mc the context to use.
* @throws ArithmeticException if the result is inexact but the
* rounding mode is {@code UNNECESSARY}.
* @throws NumberFormatException if {@code in} is not a valid
* representation of a {@code BigDecimal} or the defined subarray
@@ -677,14 +678,14 @@
* Translates a character array representation of a
* {@code BigDecimal} into a {@code BigDecimal}, accepting the
* same sequence of characters as the {@link #BigDecimal(String)}
* constructor.
*
- * <p>Note that if the sequence of characters is already available
+ * @implNote If the sequence of characters is already available
* as a character array, using this constructor is faster than
* converting the {@code char} array to string and using the
- * {@code BigDecimal(String)} constructor .
+ * {@code BigDecimal(String)} constructor.
*
* @param in {@code char} array that is the source of characters.
* @throws NumberFormatException if {@code in} is not a valid
* representation of a {@code BigDecimal}.
* @since 1.5
@@ -698,14 +699,14 @@
* {@code BigDecimal} into a {@code BigDecimal}, accepting the
* same sequence of characters as the {@link #BigDecimal(String)}
* constructor and with rounding according to the context
* settings.
*
- * <p>Note that if the sequence of characters is already available
+ * @implNote If the sequence of characters is already available
* as a character array, using this constructor is faster than
* converting the {@code char} array to string and using the
- * {@code BigDecimal(String)} constructor .
+ * {@code BigDecimal(String)} constructor.
*
* @param in {@code char} array that is the source of characters.
* @param mc the context to use.
* @throws ArithmeticException if the result is inexact but the
* rounding mode is {@code UNNECESSARY}.
@@ -803,11 +804,11 @@
* "1234.5E-4" [12345,5]
* "0E+7" [0,-7]
* "-0" [0,0]
* </pre>
*
- * <p>Note: For values other than {@code float} and
+ * @apiNote For values other than {@code float} and
* {@code double} NaN and ±Infinity, this constructor is
* compatible with the values returned by {@link Float#toString}
* and {@link Double#toString}. This is generally the preferred
* way to convert a {@code float} or {@code double} into a
* BigDecimal, as it doesn't suffer from the unpredictability of
@@ -857,17 +858,17 @@
* actually equal to
* 0.1000000000000000055511151231257827021181583404541015625.
* This is because 0.1 cannot be represented exactly as a
* {@code double} (or, for that matter, as a binary fraction of
* any finite length). Thus, the value that is being passed
- * <i>in</i> to the constructor is not exactly equal to 0.1,
+ * <em>in</em> to the constructor is not exactly equal to 0.1,
* appearances notwithstanding.
*
* <li>
* The {@code String} constructor, on the other hand, is
* perfectly predictable: writing {@code new BigDecimal("0.1")}
- * creates a {@code BigDecimal} which is <i>exactly</i> equal to
+ * creates a {@code BigDecimal} which is <em>exactly</em> equal to
* 0.1, as one would expect. Therefore, it is generally
* recommended that the {@linkplain #BigDecimal(String)
* String constructor} be used in preference to this one.
*
* <li>
@@ -1197,14 +1198,15 @@
// Static Factory Methods
/**
* Translates a {@code long} unscaled value and an
- * {@code int} scale into a {@code BigDecimal}. This
- * {@literal "static factory method"} is provided in preference to
- * a ({@code long}, {@code int}) constructor because it
- * allows for reuse of frequently used {@code BigDecimal} values..
+ * {@code int} scale into a {@code BigDecimal}.
+ *
+ * @apiNote This static factory method is provided in preference
+ * to a ({@code long}, {@code int}) constructor because it allows
+ * for reuse of frequently used {@code BigDecimal} values.
*
* @param unscaledVal unscaled value of the {@code BigDecimal}.
* @param scale scale of the {@code BigDecimal}.
* @return a {@code BigDecimal} whose value is
* <code>(unscaledVal × 10<sup>-scale</sup>)</code>.
@@ -1220,14 +1222,15 @@
unscaledVal, scale, 0);
}
/**
* Translates a {@code long} value into a {@code BigDecimal}
- * with a scale of zero. This {@literal "static factory method"}
- * is provided in preference to a ({@code long}) constructor
- * because it allows for reuse of frequently used
- * {@code BigDecimal} values.
+ * with a scale of zero.
+ *
+ * @apiNote This static factory method is provided in preference
+ * to a ({@code long}) constructor because it allows for reuse of
+ * frequently used {@code BigDecimal} values.
*
* @param val value of the {@code BigDecimal}.
* @return a {@code BigDecimal} whose value is {@code val}.
*/
public static BigDecimal valueOf(long val) {
@@ -1268,15 +1271,15 @@
/**
* Translates a {@code double} into a {@code BigDecimal}, using
* the {@code double}'s canonical string representation provided
* by the {@link Double#toString(double)} method.
*
- * <p><b>Note:</b> This is generally the preferred way to convert
- * a {@code double} (or {@code float}) into a
- * {@code BigDecimal}, as the value returned is equal to that
- * resulting from constructing a {@code BigDecimal} from the
- * result of using {@link Double#toString(double)}.
+ * @apiNote This is generally the preferred way to convert a
+ * {@code double} (or {@code float}) into a {@code BigDecimal}, as
+ * the value returned is equal to that resulting from constructing
+ * a {@code BigDecimal} from the result of using {@link
+ * Double#toString(double)}.
*
* @param val {@code double} to convert to a {@code BigDecimal}.
* @return a {@code BigDecimal} whose value is equal to or approximately
* equal to the value of {@code val}.
* @throws NumberFormatException if {@code val} is infinite or NaN.
@@ -1894,11 +1897,11 @@
/**
* Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
*
* <p>The remainder is given by
* {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
- * Note that this is not the modulo operation (the result can be
+ * Note that this is <em>not</em> the modulo operation (the result can be
* negative).
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return {@code this % divisor}.
* @throws ArithmeticException if {@code divisor==0}
@@ -2035,10 +2038,11 @@
* expansion of the exact result
* @throws ArithmeticException if
* {@code (mc.getRoundingMode()==RoundingMode.UNNECESSARY}) and
* the exact result cannot fit in {@code mc.getPrecision()}
* digits.
+ * @see BigInteger#sqrt()
* @since 9
*/
public BigDecimal sqrt(MathContext mc) {
int signum = signum();
if (signum == 1) {
@@ -2692,12 +2696,12 @@
* scale is reduced by the operation, the unscaled value must be
* divided (rather than multiplied), and the value may be changed;
* in this case, the specified rounding mode is applied to the
* division.
*
- * <p>Note that since BigDecimal objects are immutable, calls of
- * this method do <i>not</i> result in the original object being
+ * @apiNote Since BigDecimal objects are immutable, calls of
+ * this method do <em>not</em> result in the original object being
* modified, contrary to the usual convention of having methods
* named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
* Instead, {@code setScale} returns an object with the proper
* scale; the returned object may or may not be newly allocated.
*
@@ -2725,12 +2729,12 @@
* scale is reduced by the operation, the unscaled value must be
* divided (rather than multiplied), and the value may be changed;
* in this case, the specified rounding mode is applied to the
* division.
*
- * <p>Note that since BigDecimal objects are immutable, calls of
- * this method do <i>not</i> result in the original object being
+ * @apiNote Since BigDecimal objects are immutable, calls of
+ * this method do <em>not</em> result in the original object being
* modified, contrary to the usual convention of having methods
* named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
* Instead, {@code setScale} returns an object with the proper
* scale; the returned object may or may not be newly allocated.
*
@@ -2820,12 +2824,12 @@
*
* <p>This method returns the same result as the two-argument
* versions of {@code setScale}, but saves the caller the trouble
* of specifying a rounding mode in cases where it is irrelevant.
*
- * <p>Note that since {@code BigDecimal} objects are immutable,
- * calls of this method do <i>not</i> result in the original
+ * @apiNote Since {@code BigDecimal} objects are immutable,
+ * calls of this method do <em>not</em> result in the original
* object being modified, contrary to the usual convention of
* having methods named <code>set<i>X</i></code> mutate field
* <i>{@code X}</i>. Instead, {@code setScale} returns an
* object with the proper scale; the returned object may or may
* not be newly allocated.
@@ -3089,11 +3093,11 @@
// Hash Function
/**
* Returns the hash code for this {@code BigDecimal}. Note that
* two {@code BigDecimal} objects that are numerically equal but
- * differ in scale (like 2.0 and 2.00) will generally <i>not</i>
+ * differ in scale (like 2.0 and 2.00) will generally <em>not</em>
* have the same hash code.
*
* @return hash code for this {@code BigDecimal}.
* @see #equals(Object)
*/
@@ -3340,11 +3344,11 @@
/**
* Converts this {@code BigDecimal} to a {@code BigInteger}.
* This conversion is analogous to the
* <i>narrowing primitive conversion</i> from {@code double} to
- * {@code long} as defined in section 5.1.3 of
+ * {@code long} as defined in
* <cite>The Java™ Language Specification</cite>:
* any fractional part of this
* {@code BigDecimal} will be discarded. Note that this
* conversion can lose information about the precision of the
* {@code BigDecimal} value.
@@ -3352,10 +3356,11 @@
* To have an exception thrown if the conversion is inexact (in
* other words if a nonzero fractional part is discarded), use the
* {@link #toBigIntegerExact()} method.
*
* @return this {@code BigDecimal} converted to a {@code BigInteger}.
+ * @jls 5.1.3 Narrowing Primitive Conversion
*/
public BigInteger toBigInteger() {
// force to an integer, quietly
return this.setScale(0, ROUND_DOWN).inflated();
}
@@ -3377,21 +3382,22 @@
/**
* Converts this {@code BigDecimal} to a {@code long}.
* This conversion is analogous to the
* <i>narrowing primitive conversion</i> from {@code double} to
- * {@code short} as defined in section 5.1.3 of
+ * {@code short} as defined in
* <cite>The Java™ Language Specification</cite>:
* any fractional part of this
* {@code BigDecimal} will be discarded, and if the resulting
* "{@code BigInteger}" is too big to fit in a
* {@code long}, only the low-order 64 bits are returned.
* Note that this conversion can lose information about the
* overall magnitude and precision of this {@code BigDecimal} value as well
* as return a result with the opposite sign.
*
* @return this {@code BigDecimal} converted to a {@code long}.
+ * @jls 5.1.3 Narrowing Primitive Conversion
*/
@Override
public long longValue(){
return (intCompact != INFLATED && scale == 0) ?
intCompact:
@@ -3446,21 +3452,22 @@
/**
* Converts this {@code BigDecimal} to an {@code int}.
* This conversion is analogous to the
* <i>narrowing primitive conversion</i> from {@code double} to
- * {@code short} as defined in section 5.1.3 of
+ * {@code short} as defined in
* <cite>The Java™ Language Specification</cite>:
* any fractional part of this
* {@code BigDecimal} will be discarded, and if the resulting
* "{@code BigInteger}" is too big to fit in an
* {@code int}, only the low-order 32 bits are returned.
* Note that this conversion can lose information about the
* overall magnitude and precision of this {@code BigDecimal}
* value as well as return a result with the opposite sign.
*
* @return this {@code BigDecimal} converted to an {@code int}.
+ * @jls 5.1.3 Narrowing Primitive Conversion
*/
@Override
public int intValue() {
return (intCompact != INFLATED && scale == 0) ?
(int)intCompact :
@@ -3529,21 +3536,22 @@
/**
* Converts this {@code BigDecimal} to a {@code float}.
* This conversion is similar to the
* <i>narrowing primitive conversion</i> from {@code double} to
- * {@code float} as defined in section 5.1.3 of
+ * {@code float} as defined in
* <cite>The Java™ Language Specification</cite>:
* if this {@code BigDecimal} has too great a
* magnitude to represent as a {@code float}, it will be
* converted to {@link Float#NEGATIVE_INFINITY} or {@link
* Float#POSITIVE_INFINITY} as appropriate. Note that even when
* the return value is finite, this conversion can lose
* information about the precision of the {@code BigDecimal}
* value.
*
* @return this {@code BigDecimal} converted to a {@code float}.
+ * @jls 5.1.3 Narrowing Primitive Conversion
*/
@Override
public float floatValue(){
if(intCompact != INFLATED) {
if (scale == 0) {
@@ -3573,21 +3581,22 @@
/**
* Converts this {@code BigDecimal} to a {@code double}.
* This conversion is similar to the
* <i>narrowing primitive conversion</i> from {@code double} to
- * {@code float} as defined in section 5.1.3 of
+ * {@code float} as defined
* <cite>The Java™ Language Specification</cite>:
* if this {@code BigDecimal} has too great a
* magnitude represent as a {@code double}, it will be
* converted to {@link Double#NEGATIVE_INFINITY} or {@link
* Double#POSITIVE_INFINITY} as appropriate. Note that even when
* the return value is finite, this conversion can lose
* information about the precision of the {@code BigDecimal}
* value.
*
* @return this {@code BigDecimal} converted to a {@code double}.
+ * @jls 5.1.3 Narrowing Primitive Conversion
*/
@Override
public double doubleValue(){
if(intCompact != INFLATED) {
if (scale == 0) {
< prev index next >