Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

MathContext

class MathContext : Serializable
kotlin.Any
   ↳ java.math.MathContext

Immutable objects describing settings such as rounding mode and digit precision for the numerical operations provided by class BigDecimal.

Summary

Public constructors
<init>(precision: Int)

Constructs a new MathContext with the specified precision and with the rounding mode RoundingMode#HALF_UP.

<init>(precision: Int, roundingMode: RoundingMode!)

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

Constructs a new MathContext from a string.

Public methods
Boolean
equals(other: Any?)

Returns true if x is a MathContext with the same precision setting and the same rounding mode as this MathContext instance.

Int

Returns the precision.

RoundingMode!

Returns the rounding mode.

Int

Returns the hash code for this MathContext instance.

String

Returns the string representation for this MathContext instance.

Properties
static MathContext!

A MathContext which corresponds to the IEEE 754 quadruple decimal precision format: 34 digit precision and RoundingMode#HALF_EVEN rounding.

static MathContext!

A MathContext which corresponds to the IEEE 754 single decimal precision format: 7 digit precision and RoundingMode#HALF_EVEN rounding.

static MathContext!

A MathContext which corresponds to the IEEE 754 double decimal precision format: 16 digit precision and RoundingMode#HALF_EVEN rounding.

static MathContext!

A MathContext for unlimited precision with RoundingMode#HALF_UP rounding.

Public constructors

<init>

Added in API level 1
MathContext(precision: Int)

Constructs a new MathContext with the specified precision and with the rounding mode RoundingMode#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.

Parameters
precision Int: the precision for the new MathContext.
Exceptions
java.lang.IllegalArgumentException if precision < 0.

<init>

Added in API level 1
MathContext(
    precision: Int,
    roundingMode: RoundingMode!)

Constructs a new 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.

Parameters
precision Int: the precision for the new MathContext.
roundingMode RoundingMode!: the rounding mode for the new MathContext.
Exceptions
java.lang.IllegalArgumentException if precision < 0.
java.lang.NullPointerException if roundingMode is null.

<init>

Added in API level 1
MathContext(s: String!)

Constructs a new 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 toString method.

Exceptions
java.lang.IllegalArgumentException if the string is not in the correct format or if the precision specified is < 0.

Public methods

equals

Added in API level 1
fun equals(other: Any?): Boolean

Returns true if x is a MathContext with the same precision setting and the same rounding mode as this MathContext instance.

Parameters
obj the reference object with which to compare.
x object to be compared.
Return
Boolean true if this MathContext instance is equal to the x argument; false otherwise.

getPrecision

Added in API level 1
fun getPrecision(): Int

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
Int the precision.

getRoundingMode

Added in API level 1
fun getRoundingMode(): RoundingMode!

Returns the rounding mode. The rounding mode is the strategy to be used to round results.

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

Return
RoundingMode! the rounding mode.

hashCode

Added in API level 1
fun hashCode(): Int

Returns the hash code for this MathContext instance.

Return
Int the hash code for this MathContext.

toString

Added in API level 1
fun toString(): String

Returns the string representation for this MathContext instance. The string has the form "precision=<precision> roundingMode=<roundingMode>" where <precision> is an integer describing the number of digits used for operations and <roundingMode> is the string representation of the rounding mode.

Return
String a string representation for this MathContext instance

Properties

DECIMAL128

Added in API level 1
static val DECIMAL128: MathContext!

A MathContext which corresponds to the IEEE 754 quadruple decimal precision format: 34 digit precision and RoundingMode#HALF_EVEN rounding.

DECIMAL32

Added in API level 1
static val DECIMAL32: MathContext!

A MathContext which corresponds to the IEEE 754 single decimal precision format: 7 digit precision and RoundingMode#HALF_EVEN rounding.

DECIMAL64

Added in API level 1
static val DECIMAL64: MathContext!

A MathContext which corresponds to the IEEE 754 double decimal precision format: 16 digit precision and RoundingMode#HALF_EVEN rounding.

UNLIMITED

Added in API level 1
static val UNLIMITED: MathContext!

A MathContext for unlimited precision with RoundingMode#HALF_UP rounding.