FileDocCategorySizeDatePackage
MathContext.javaAPI DocAndroid 1.5 API12912Wed May 06 22:41:04 BST 2009java.math

MathContext

public final class MathContext extends Object implements Serializable
Immutable objects describing settings such as rounding mode and digit precision for the numerical operations provided by class {@link BigDecimal}.
since
Android 1.0

Fields Summary
public static final MathContext
DECIMAL128
A {@code MathContext} which corresponds to the IEEE 754r quadruple decimal precision format: 34 digit precision and {@link RoundingMode#HALF_EVEN} rounding.
public static final MathContext
DECIMAL32
A {@code MathContext} which corresponds to the IEEE 754r single decimal precision format: 7 digit precision and {@link RoundingMode#HALF_EVEN} rounding.
public static final MathContext
DECIMAL64
A {@code MathContext} which corresponds to the IEEE 754r double decimal precision format: 16 digit precision and {@link RoundingMode#HALF_EVEN} rounding.
public static final MathContext
UNLIMITED
A {@code MathContext} for unlimited precision with {@link RoundingMode#HALF_UP} rounding.
private static final long
serialVersionUID
This is the serialVersionUID used by the sun implementation
private int
precision
The number of digits to be used for an operation; results are rounded to this precision.
private RoundingMode
roundingMode
A {@code RoundingMode} object which specifies the algorithm to be used for rounding.
private static final char[]
chPrecision
An array of {@code char} containing: {@code 'p','r','e','c','i','s','i','o','n','='}. It's used to improve the methods related to {@code String} conversion.
private static final char[]
chRoundingMode
An array of {@code char} containing: {@code 'r','o','u','n','d','i','n','g','M','o','d','e','='}. It's used to improve the methods related to {@code String} conversion.
Constructors Summary
public MathContext(int precision)
Constructs a new {@code MathContext} with the specified precision and with the rounding mode {@link RoundingMode#HALF_UP HALF_UP}. If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.

param
precision the precision for the new {@code MathContext}.
throws
IllegalArgumentException if {@code precision < 0}.
since
Android 1.0


    /* Constructors */

                                                                                               
       
        // BEGIN android-note
        // parameter names changed.
        // END android-note
        this(precision, RoundingMode.HALF_UP);
    
public MathContext(int precision, RoundingMode roundingMode)
Constructs a new {@code MathContext} with the specified precision and with the specified rounding mode. If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.

param
precision the precision for the new {@code MathContext}.
param
roundingMode the rounding mode for the new {@code MathContext}.
throws
IllegalArgumentException if {@code precision < 0}.
throws
NullPointerException if {@code roundingMode} is {@code null}.
since
Android 1.0

        // BEGIN android-note
        // parameter names changed.
        // END android-note
        if (precision < 0) {
            // math.0C=Digits < 0
            throw new IllegalArgumentException(Messages.getString("math.0C")); //$NON-NLS-1$
        }
        if (roundingMode == null) {
            // math.0D=null RoundingMode
            throw new NullPointerException(Messages.getString("math.0D")); //$NON-NLS-1$
        }
        this.precision = precision;
        this.roundingMode = roundingMode;
    
public MathContext(String val)
Constructs a new {@code MathContext} from a string. The string has to specify the precision and the rounding mode to be used and has to follow the following syntax: "precision=<precision> roundingMode=<roundingMode>" This is the same form as the one returned by the {@link #toString} method.

param
val a string describing the precision and rounding mode for the new {@code MathContext}.
throws
IllegalArgumentException if the string is not in the correct format or if the precision specified is < 0.
since
Android 1.0

        char[] charVal = val.toCharArray();
        int i; // Index of charVal
        int j; // Index of chRoundingMode
        int digit; // It will contain the digit parsed

        if ((charVal.length < 27) || (charVal.length > 45)) {
            // math.0E=bad string format
            throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
        }
        // Parsing "precision=" String
        for (i = 0; (i < chPrecision.length) && (charVal[i] == chPrecision[i]); i++) {
            ;
        }

        if (i < chPrecision.length) {
            // math.0E=bad string format
            throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
        }
        // Parsing the value for "precision="...
        digit = Character.digit(charVal[i], 10);
        if (digit == -1) {
            // math.0E=bad string format
            throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
        }
        // BEGIN android-changed
        this.precision = digit;
        // END android-changed
        i++;

        do {
            digit = Character.digit(charVal[i], 10);
            if (digit == -1) {
                if (charVal[i] == ' ") {
                    // It parsed all the digits
                    i++;
                    break;
                }
                // It isn't  a valid digit, and isn't a white space
                // math.0E=bad string format
                throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
            }
            // Accumulating the value parsed
            this.precision = this.precision * 10 + digit;
            if (this.precision < 0) {
                // math.0E=bad string format
                throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
            }
            i++;
        } while (true);
        // Parsing "roundingMode="
        for (j = 0; (j < chRoundingMode.length)
                && (charVal[i] == chRoundingMode[j]); i++, j++) {
            ;
        }

        if (j < chRoundingMode.length) {
            // math.0E=bad string format
            throw new IllegalArgumentException(Messages.getString("math.0E")); //$NON-NLS-1$
        }
        // Parsing the value for "roundingMode"...
        this.roundingMode = RoundingMode.valueOf(String.valueOf(charVal, i,
                charVal.length - i));
    
Methods Summary
public booleanequals(java.lang.Object x)
Returns true if x is a {@code MathContext} with the same precision setting and the same rounding mode as this {@code MathContext} instance.

param
x object to be compared.
return
{@code true} if this {@code MathContext} instance is equal to the {@code x} argument; {@code false} otherwise.
since
Android 1.0

        return ((x instanceof MathContext)
                && (((MathContext) x).getPrecision() == precision) && (((MathContext) x)
                .getRoundingMode() == roundingMode));
    
public intgetPrecision()
Returns the precision. The precision is the number of digits used for an operation. Results are rounded to this precision. The precision is guaranteed to be non negative. If the precision is zero, then the computations have to be performed exact, results are not rounded in this case.

return
the precision.
since
Android 1.0

        return precision;
    
public java.math.RoundingModegetRoundingMode()
Returns the rounding mode. The rounding mode is the strategy to be used to round results.

The rounding mode is one of {@link RoundingMode#UP}, {@link RoundingMode#DOWN}, {@link RoundingMode#CEILING}, {@link RoundingMode#FLOOR}, {@link RoundingMode#HALF_UP}, {@link RoundingMode#HALF_DOWN}, {@link RoundingMode#HALF_EVEN}, or {@link RoundingMode#UNNECESSARY}.

return
the rounding mode.
since
Android 1.0

        return roundingMode;
    
public inthashCode()
Returns the hash code for this {@code MathContext} instance.

return
the hash code for this {@code MathContext}.
since
Android 1.0

        // Make place for the necessary bits to represent 8 rounding modes
        return ((precision << 3) | roundingMode.ordinal());
    
private voidreadObject(java.io.ObjectInputStream s)
Makes checks upon deserialization of a {@code MathContext} instance. Checks whether {@code precision >= 0} and {@code roundingMode != null}

throws
StreamCorruptedException if {@code precision < 0}
throws
StreamCorruptedException if {@code roundingMode == null}

        s.defaultReadObject();
        if (precision < 0) {
            // math.0F=bad precision value
            throw new StreamCorruptedException(Messages.getString("math.0F")); //$NON-NLS-1$
        }
        if (roundingMode == null) {
            // math.10=null roundingMode
            throw new StreamCorruptedException(Messages.getString("math.10")); //$NON-NLS-1$
        }
    
public java.lang.StringtoString()
Returns the string representation for this {@code MathContext} instance. The string has the form {@code "precision=<precision> roundingMode=<roundingMode>" } where {@code <precision>} is an integer describing the number of digits used for operations and {@code <roundingMode>} is the string representation of the rounding mode.

return
a string representation for this {@code MathContext} instance
since
Android 1.0

        StringBuffer sb = new StringBuffer(45);

        sb.append(chPrecision);
        sb.append(precision);
        sb.append(' ");
        sb.append(chRoundingMode);
        sb.append(roundingMode);
        return sb.toString();