Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Fields](https://developer.android.com/reference/java/math/MathContext#lfields) \| [Ctors](https://developer.android.com/reference/java/math/MathContext#pubctors) \| [Methods](https://developer.android.com/reference/java/math/MathContext#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/math/MathContext#inhmethods)

# MathContext

` public final class MathContext `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/io/Serializable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.math.MathContext |

<br />

*** ** * ** ***

Immutable objects which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the `https://developer.android.com/reference/java/math/BigDecimal` class.

The base-independent settings are:

1. `precision`: the number of digits to be used for an operation; results are rounded to this precision
2. `roundingMode`: a `https://developer.android.com/reference/java/math/RoundingMode` object which specifies the algorithm to be used for rounding.

<br />

**See also:**

- `https://developer.android.com/reference/java/math/BigDecimal`
- `https://developer.android.com/reference/java/math/RoundingMode`

## Summary

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/java/math/MathContext` | `https://developer.android.com/reference/java/math/MathContext#DECIMAL128` A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal128 format, 34 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. |
| ` public static final https://developer.android.com/reference/java/math/MathContext` | `https://developer.android.com/reference/java/math/MathContext#DECIMAL32` A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal32 format, 7 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. |
| ` public static final https://developer.android.com/reference/java/math/MathContext` | `https://developer.android.com/reference/java/math/MathContext#DECIMAL64` A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal64 format, 16 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. |
| ` public static final https://developer.android.com/reference/java/math/MathContext` | `https://developer.android.com/reference/java/math/MathContext#UNLIMITED` A `MathContext` object whose settings have the values required for unlimited precision arithmetic. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/math/MathContext#MathContext(int)(int setPrecision) ` Constructs a new `MathContext` with the specified precision and the `https://developer.android.com/reference/java/math/RoundingMode#HALF_UP` rounding mode. |
| ` https://developer.android.com/reference/java/math/MathContext#MathContext(int,%20java.math.RoundingMode)(int setPrecision, https://developer.android.com/reference/java/math/RoundingMode setRoundingMode) ` Constructs a new `MathContext` with a specified precision and rounding mode. |
| ` https://developer.android.com/reference/java/math/MathContext#MathContext(java.lang.String)(https://developer.android.com/reference/java/lang/String val) ` Constructs a new `MathContext` from a string. |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/java/math/MathContext#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object x) ` Compares this `MathContext` with the specified `Object` for equality. |
| ` int` | ` https://developer.android.com/reference/java/math/MathContext#getPrecision()() ` Returns the `precision` setting. |
| ` https://developer.android.com/reference/java/math/RoundingMode` | ` https://developer.android.com/reference/java/math/MathContext#getRoundingMode()() ` Returns the roundingMode setting. |
| ` int` | ` https://developer.android.com/reference/java/math/MathContext#hashCode()() ` Returns the hash code for this `MathContext`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/math/MathContext#toString()() ` Returns the string representation of this `MathContext`. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Fields

### DECIMAL128

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final MathContext DECIMAL128
```

A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal128 format, 34 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. Note the exponent range of decimal64 is *not* used for rounding.

<br />

### DECIMAL32

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final MathContext DECIMAL32
```

A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal32 format, 7 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. Note the exponent range of decimal32 is *not* used for rounding.

<br />

### DECIMAL64

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final MathContext DECIMAL64
```

A `MathContext` object with a precision setting matching the precision of the IEEE 754-2019 decimal64 format, 16 digits, and a rounding mode of `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`. Note the exponent range of decimal64 is *not* used for rounding.

<br />

### UNLIMITED

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final MathContext UNLIMITED
```

A `MathContext` object whose settings have the values required for unlimited precision arithmetic. The values of the settings are: `precision=0 roundingMode=HALF_UP`

<br />

## Public constructors

### MathContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MathContext (int setPrecision)
```

Constructs a new `MathContext` with the specified precision and the `https://developer.android.com/reference/java/math/RoundingMode#HALF_UP` rounding mode.

<br />

| Parameters ||
|---|---|
| `setPrecision` | `int`: The non-negative `int` precision setting. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the `setPrecision` parameter is less than zero. |

### MathContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MathContext (int setPrecision, 
                RoundingMode setRoundingMode)
```

Constructs a new `MathContext` with a specified precision and rounding mode.

<br />

| Parameters ||
|---|---|
| `setPrecision` | `int`: The non-negative `int` precision setting. <br /> |
| `setRoundingMode` | `RoundingMode`: The rounding mode to use. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the `setPrecision` parameter is less than zero. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the rounding mode argument is `null` |

### MathContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MathContext (String val)
```

Constructs a new `MathContext` from a string. The string must be in the same format as that produced by the `https://developer.android.com/reference/java/math/MathContext#toString()` method.

An `IllegalArgumentException` is thrown if the precision section of the string is out of range (`< 0`) or the string is not in the format created by the `https://developer.android.com/reference/java/math/MathContext#toString()` method.

<br />

| Parameters ||
|---|---|
| `val` | `String`: The string to be parsed <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the precision section is out of range or of incorrect format |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the argument is `null` |

## Public methods

### equals

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean equals (Object x)
```

Compares this `MathContext` with the specified `Object` for equality.

<br />

| Parameters ||
|---|---|
| `x` | `Object`: `Object` to which this `MathContext` is to be compared. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if and only if the specified `Object` is a `MathContext` object which has exactly the same settings as this object <br /> |

### getPrecision

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getPrecision ()
```

Returns the `precision` setting. This value is always non-negative.

<br />

| Returns ||
|---|---|
| `int` | an `int` which is the value of the `precision` setting <br /> |

### getRoundingMode

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public RoundingMode getRoundingMode ()
```

Returns the roundingMode setting. This will be one of `https://developer.android.com/reference/java/math/RoundingMode#CEILING`, `https://developer.android.com/reference/java/math/RoundingMode#DOWN`, `https://developer.android.com/reference/java/math/RoundingMode#FLOOR`, `https://developer.android.com/reference/java/math/RoundingMode#HALF_DOWN`, `https://developer.android.com/reference/java/math/RoundingMode#HALF_EVEN`, `https://developer.android.com/reference/java/math/RoundingMode#HALF_UP`, `https://developer.android.com/reference/java/math/RoundingMode#UNNECESSARY`, or `https://developer.android.com/reference/java/math/RoundingMode#UP`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/math/RoundingMode` | a `RoundingMode` object which is the value of the `roundingMode` setting <br /> |

### hashCode

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int hashCode ()
```

Returns the hash code for this `MathContext`.

<br />

| Returns ||
|---|---|
| `int` | hash code for this `MathContext` <br /> |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns the string representation of this `MathContext`. The `String` returned represents the settings of the `MathContext` object as two space-delimited words (separated by a single space character, `'\u0020'`, and with no leading or trailing white space), as follows:

1. The string `"precision="`, immediately followed by the value of the precision setting as a numeric string as if generated by the `https://developer.android.com/reference/java/lang/Integer#toString(int)` method.
2. The string `"roundingMode="`, immediately followed by the value of the `roundingMode` setting as a word. This word will be the same as the name of the corresponding public constant in the `https://developer.android.com/reference/java/math/RoundingMode` enum.

For example:

```
precision=9 roundingMode=HALF_UP
```
Additional words may be appended to the result of `toString` in the future if more properties are added to this class.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a `String` representing the context settings <br /> |