Decimal

Represents a mathematical decimal number (ℚ∩ℝ_decimal), with exact arithmetic operations that don't overflow and consistent behavior across all platforms.


Decimal vs Double/Float

Decimal vs Double/Float

The Decimal type is designed to replace floating-point types (Double and Float) where correctness matters. Floating-point binary representation cannot exactly represent most base-10 decimal values, leading to subtle rounding errors.

The Decimal type stores values as an exact scaled integer, avoiding all floating-point imprecision.


Why no division?

Why no division?

The set of terminating decimal numbers is not closed under division: dividing two terminating decimals can produce a non-terminating result (e.g., 1 / 3 = 0.333...). Division is therefore intentionally excluded from this type.

This is consistent with how Integer excludes operations that would leave the set of integers (square roots, noninteger divisions without remainder).


Key features

Key features

Since

5.2.0

Types

Link copied to clipboard
object Companion

Contains class-level declarations for the Decimal type.

Functions

Link copied to clipboard
operator fun compareTo(other: Decimal): Int

Compares this decimal with the other one for order.

Link copied to clipboard
operator override fun equals(other: Any?): Boolean

Returns true if the other object is a Decimal representing the same numeric value as this one, or returns false otherwise.

Link copied to clipboard
override fun hashCode(): Int

Returns a hash code value for this decimal.

Link copied to clipboard
operator fun minus(other: Decimal): Decimal

Subtracts the other decimal from this one.

Link copied to clipboard
operator fun plus(other: Decimal): Decimal

Adds the other decimal to this one.

Link copied to clipboard
operator fun times(other: Decimal): Decimal

Multiplies this decimal by the other one.

Link copied to clipboard
override fun toString(): String

Returns the canonical decimal string representation of this decimal.

Link copied to clipboard
operator fun unaryMinus(): Decimal

Returns the negative of this decimal.