⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bigdecimal.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *	       maintain its overall value.     * @throws ArithmeticException <tt>scale</tt> is negative, or     * 	       the specified scaling operation would require rounding.     * @see    #setScale(int, int)     */    public BigDecimal setScale(int scale) {	return setScale(scale, ROUND_UNNECESSARY);    }    // Decimal Point Motion Operations    /**     * Returns a BigDecimal which is equivalent to this one with the decimal     * point moved n places to the left.  If n is non-negative, the call merely     * adds n to the scale.  If n is negative, the call is equivalent to     * movePointRight(-n).  (The BigDecimal returned by this call has value     * <tt>(this * 10<sup>-n</sup>)</tt> and scale     * <tt>max(this.scale()+n, 0)</tt>.)     *     * @param  n number of places to move the decimal point to the left.     * @return a BigDecimal which is equivalent to this one with the decimal     *	       point moved <tt>n</tt> places to the left.     */    public BigDecimal movePointLeft(int n){	return (n>=0 ? new BigDecimal(intVal, scale+n) : movePointRight(-n));    }    /**     * Moves the decimal point the specified number of places to the right.     * If this BigDecimal's scale is &gt;= <tt>n</tt>, the call merely     * subtracts <tt>n</tt> from the scale; otherwise, it sets the scale to     * zero, and multiplies the integer value by     * <tt>10<sup>(n - this.scale)</sup></tt>.  If <tt>n</tt>     * is negative, the call is equivalent to <tt>movePointLeft(-n)</tt>. (The     * BigDecimal returned by this call has value     * <tt>(this * 10<sup>n</sup>)</tt> and scale     * <tt>max(this.scale()-n, 0)</tt>.)     *     * @param  n number of places to move the decimal point to the right.     * @return a BigDecimal which is equivalent to this one with the decimal     *         point moved <tt>n</tt> places to the right.     */    public BigDecimal movePointRight(int n){	return (scale >= n ? new BigDecimal(intVal, scale-n)		           : new BigDecimal(timesTenToThe(intVal, n-scale),0));    }    // Comparison Operations    /**     * Compares this BigDecimal with the specified BigDecimal.   Two     * BigDecimals that are equal in value but have a different scale (like     * 2.0 and 2.00) are considered equal by this method.  This method is     * provided in preference to individual methods for each of the six     * boolean comparison operators (&lt;, ==, &gt;, &gt;=, !=, &lt;=).  The     * suggested idiom for performing these comparisons is:     * <tt>(x.compareTo(y)</tt> &lt;<i>op</i>&gt; <tt>0)</tt>,     * where &lt;<i>op</i>&gt; is one of the six comparison operators.     *     * @param  val BigDecimal to which this BigDecimal is to be compared.     * @return -1, 0 or 1 as this BigDecimal is numerically less than, equal     *         to, or greater than <tt>val</tt>.     */    public int compareTo(BigDecimal val){	/* Optimization: would run fine without the next three lines */	int sigDiff = signum() - val.signum();	if (sigDiff != 0)	    return (sigDiff > 0 ? 1 : -1);	/* If signs match, scale and compare intVals */	BigDecimal arg[] = new BigDecimal[2];	arg[0] = this;	arg[1] = val;	matchScale(arg);	return arg[0].intVal.compareTo(arg[1].intVal);    }    /**     * Compares this BigDecimal with the specified Object.  If the Object is a     * BigDecimal, this method behaves like {@link #compareTo compareTo}.     * Otherwise, it throws a <tt>ClassCastException</tt> (as BigDecimals are     * comparable only to other BigDecimals).     *     * @param  o Object to which this BigDecimal is to be compared.     * @return a negative number, zero, or a positive number as this     *	       BigDecimal is numerically less than, equal to, or greater     *	       than <tt>o</tt>, which must be a BigDecimal.     * @throws ClassCastException <tt>o</tt> is not a BigDecimal.     * @see    #compareTo(java.math.BigDecimal)     * @see    Comparable     * @since  1.2     */    public int compareTo(Object o) {	return compareTo((BigDecimal)o);    }    /**     * Compares this BigDecimal with the specified Object for     * equality.  Unlike {@link #compareTo compareTo}, this method     * considers two BigDecimals equal only if they are equal in value     * and scale (thus 2.0 is not equal to 2.00 when compared by this     * method).     *     * @param  x Object to which this BigDecimal is to be compared.     * @return <tt>true</tt> if and only if the specified Object is a     *	       BigDecimal whose value and scale are equal to this BigDecimal's.     * @see    #compareTo(java.math.BigDecimal)     */    public boolean equals(Object x){	if (!(x instanceof BigDecimal))	    return false;	BigDecimal xDec = (BigDecimal) x;	return scale == xDec.scale && intVal.equals(xDec.intVal);    }    /**     * Returns the minimum of this BigDecimal and <tt>val</tt>.     *     * @param  val value with which the minimum is to be computed.     * @return the BigDecimal whose value is the lesser of this BigDecimal and      *	       <tt>val</tt>.  If they are equal, as defined by the     * 	       {@link #compareTo compareTo} method, either may be returned.     * @see    #compareTo(java.math.BigDecimal)     */    public BigDecimal min(BigDecimal val){	return (compareTo(val)<0 ? this : val);    }    /**     * Returns the maximum of this BigDecimal and <tt>val</tt>.     *     * @param  val value with which the maximum is to be computed.     * @return the BigDecimal whose value is the greater of this BigDecimal     *	       and <tt>val</tt>.  If they are equal, as defined by the     * 	       {@link #compareTo compareTo} method, either may be returned.     * @see    #compareTo(java.math.BigDecimal)     */    public BigDecimal max(BigDecimal val){	return (compareTo(val)>0 ? this : val);    }    // Hash Function    /**     * Returns the hash code for this BigDecimal.  Note that two BigDecimals     * that are numerically equal but differ in scale (like 2.0 and 2.00)     * will generally <i>not</i> have the same hash code.     *     * @return hash code for this BigDecimal.     */    public int hashCode() {	return 31*intVal.hashCode() + scale;    }    //    // add one to the least significant digit.    // in the unlikely event there is a carry out,    // deal with it.    //    private String    roundup(String val){	int i;        char[] digits = val.toCharArray();        int nDigits = digits.length;        	int q = digits[ i = (nDigits-1)];	if ( q == '9' ){	    while ( q == '9' && i > 0 ){		digits[i] = '0';		q = digits[--i];	    }	    if ( q == '9' ){		// carryout! High-order 1, rest 0s, larger exp.		digits[0] = '0';		return "1" + String.valueOf(digits);	    }	    // else fall through.	}	digits[i] = (char)(q+1);        return String.valueOf(digits);    }    // Format Converters    /**     * Returns the string representation of this BigDecimal.  The digit-to-     * character mapping provided by {@link Character#forDigit} is used.     * A leading minus sign is used to indicate sign, and the number of digits     * to the right of the decimal point is used to indicate scale.  (This     * representation is compatible with the (String) constructor.)     *     * @return String representation of this BigDecimal.     * @see    Character#forDigit     * @see    #BigDecimal(java.lang.String)     */    public String toString(){	if (scale == 0)	/* No decimal point */	    return intVal.toString();        return getValueString(signum(), intVal.abs().toString(), scale);    }    /**     * Converts this BigDecimal to a BigInteger.  This conversion is     * analogous to a <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing     * primitive conversion</i></a> from <code>double</code> to     * <code>long</code> as defined in the <a     * href="http://java.sun.com/docs/books/jls/html/">Java Language     * Specification</a>: any fractional part of this BigDecimal will     * be discarded.  Note that this conversion can lose information     * about the precision of the BigDecimal value.     *     * @return this BigDecimal converted to a BigInteger.     */    public BigInteger toBigInteger() {	return (scale==0 ? intVal			 : intVal.divide(BigInteger.valueOf(10).pow(scale)));    }    /**     * Converts this BigDecimal to an <code>int</code>.  This     * conversion is analogous to a <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing     * primitive conversion</i></a> from <code>double</code> to     * <code>short</code> as defined in the <a     * href="http://java.sun.com/docs/books/jls/html/">Java Language     * Specification</a>: any fractional part of this BigDecimal will     * be discarded, and if the resulting &quot;BigInteger&quot; is     * too big to fit in an <code>int</code>, only the low-order 32     * bits are returned.  Note that this conversion can lose     * information about the overall magnitude and precision of the     * BigDecimal value as well as return a result with the opposite     * sign.     *      * @return this BigDecimal converted to an <code>int</code>.     */    public int intValue(){	return toBigInteger().intValue();    }    /**     * Converts this BigDecimal to a <code>long</code>.  This     * conversion is analogous to a <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing     * primitive conversion</i></a> from <code>double</code> to     * <code>short</code> as defined in the <a     * href="http://java.sun.com/docs/books/jls/html/">Java Language     * Specification</a>: any fractional part of this BigDecimal will     * be discarded, and if the resulting &quot;BigInteger&quot; is     * too big to fit in a <code>long</code>, only the low-order 64     * bits are returned.  Note that this conversion can lose     * information about the overall magnitude and precision of the     * BigDecimal value as well as return a result with the opposite     * sign.     *      * @return this BigDecimal converted to an <code>long</code>.     */    public long longValue(){	return toBigInteger().longValue();    }    /**     * Converts this BigDecimal to a <code>float</code>.  This     * conversion is similar to the <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing     * primitive conversion</i></a> from <code>double</code> to     * <code>float</code> defined in the <a     * href="http://java.sun.com/docs/books/jls/html/">Java Language     * Specification</a>: if this BigDecimal has too great a magnitude     * to represent as a <code>float</code>, 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 BigDecimal value.     *      * @return this BigDecimal converted to a <code>float</code>.     */    public float floatValue(){	/* Somewhat inefficient, but guaranteed to work. */	return Float.valueOf(this.toString()).floatValue();    }    /**     * Converts this BigDecimal to a <code>double</code>.  This     * conversion is similar to the <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing     * primitive conversion</i></a> from <code>double</code> to     * <code>float</code> as defined in the <a     * href="http://java.sun.com/docs/books/jls/html/">Java Language     * Specification</a>: if this BigDecimal has too great a magnitude     * represent as a <code>double</code>, 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 BigDecimal value.     *      * @return this BigDecimal converted to a <code>double</code>.     */    public double doubleValue(){	/* Somewhat inefficient, but guaranteed to work. */	return Double.valueOf(this.toString()).doubleValue();    }    // Private "Helper" Methods        /* Returns a digit.digit string */    private String getValueString(int signum, String intString, int scale) { 	/* Insert decimal point */ 	StringBuffer buf; 	int insertionPoint = intString.length() - scale; 	if (insertionPoint == 0) {  /* Point goes right before intVal */ 	    return (signum<0 ? "-0." : "0.") + intString; 	} else if (insertionPoint > 0) { /* Point goes inside intVal */ 	    buf = new StringBuffer(intString); 	    buf.insert(insertionPoint, '.'); 	    if (signum < 0) 		buf.insert(0, '-'); 	} else { /* We must insert zeros between point and intVal */ 	    buf = new StringBuffer(3-insertionPoint + intString.length()); 	    buf.append(signum<0 ? "-0." : "0."); 	    for (int i=0; i<-insertionPoint; i++) 		buf.append('0'); 	    buf.append(intString); 	} 	return buf.toString();    }        /* Returns (a * 10^b) */    private static BigInteger timesTenToThe(BigInteger a, int b) {	return a.multiply(BigInteger.valueOf(10).pow(b));    }    /*     * If the scales of val[0] and val[1] differ, rescale (non-destructively)     * the lower-scaled BigDecimal so they match.     */    private static void matchScale(BigDecimal[] val) {	if (val[0].scale < val[1].scale)	    val[0] = val[0].setScale(val[1].scale);	else if (val[1].scale < val[0].scale)	    val[1] = val[1].setScale(val[0].scale);    }    /**     * Reconstitute the <tt>BigDecimal</tt> instance from a stream (that is,     * deserialize it).     */    private synchronized void readObject(java.io.ObjectInputStream s)        throws java.io.IOException, ClassNotFoundException {        // Read in all fields	s.defaultReadObject();        // Validate scale factor        if (scale < 0)	    throw new java.io.StreamCorruptedException(                                      "BigDecimal: Negative scale");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -