Esc
Start typing to search...

Decimal Module

Arbitrary-precision decimal numbers for exact arithmetic.

The Decimal module provides functions for working with decimal numbers that avoid the precision issues of floating-point arithmetic. Useful for financial calculations and other scenarios where exactness matters.

Common patterns

import Decimal

Decimal.fromInt 42
Decimal.round 2 3.14159d

Decimal literals

Use the d suffix for decimal literals: 3.14d, 100d.

Functions

Conversion

Decimal.fromInt

Int -> Decimal

Convert an integer to a decimal.

Example:
import Decimal

Decimal.fromInt 42
Try it

Notes: Always succeeds. Preserves exact integer value.

See also: Decimal.fromFloat, Decimal.toInt

Decimal.fromFloat

Float -> Result Decimal String

Convert a float to a decimal. May fail for special float values (NaN, infinity).

Example:
import Decimal

Decimal.fromFloat 3.14
Try it

Notes: Returns Err for NaN, infinity, or values outside decimal range.

See also: Decimal.fromInt, Decimal.toFloat

Decimal.toInt

Decimal -> Int

Convert a decimal to an integer by truncating towards zero.

Example:
import Decimal

Decimal.toInt 3.7d
Try it

Notes: Truncates fractional part. Use round/floor/ceil first for different behavior.

See also: Decimal.toFloat, Decimal.truncate

Decimal.toFloat

Decimal -> Float

Convert a decimal to a float. May lose precision.

Example:
import Decimal

Decimal.toFloat 3.14d
Try it

Notes: May lose precision for large or high-precision decimals.

See also: Decimal.toInt, Decimal.fromFloat

Rounding

Decimal.round

Int -> Decimal -> Decimal

Round a decimal to n decimal places using banker's rounding.

Example:
import Decimal

Decimal.round 2 3.14159d
Try it

Notes: Uses banker's rounding (round half to even). Negative places round to left of decimal.

See also: Decimal.floor, Decimal.ceil, Decimal.truncate

Decimal.floor

Decimal -> Decimal

Round a decimal down to the nearest integer.

Example:
import Decimal

Decimal.floor 3.7d
Try it

Notes: Always rounds towards negative infinity.

See also: Decimal.ceil, Decimal.round, Decimal.truncate

Decimal.ceil

Decimal -> Decimal

Round a decimal up to the nearest integer.

Example:
import Decimal

Decimal.ceil 3.2d
Try it

Notes: Always rounds towards positive infinity.

See also: Decimal.floor, Decimal.round, Decimal.truncate

Decimal.truncate

Decimal -> Decimal

Truncate a decimal towards zero (remove fractional part).

Example:
import Decimal

Decimal.truncate 3.7d
Try it

Notes: Equivalent to toInt but returns Decimal.

See also: Decimal.floor, Decimal.ceil, Decimal.round

Arithmetic

Decimal.abs

Decimal -> Decimal

Returns the absolute value of a decimal.

Example:
import Decimal

Decimal.abs (-5.5d)
Try it

See also: Decimal.signum, Decimal.isNegative

Decimal.signum

Decimal -> Int

Returns the sign of a decimal: -1, 0, or 1.

Example:
import Decimal

Decimal.signum (-5.5d)
Try it

Notes: Returns -1 for negative, 0 for zero, 1 for positive.

See also: Decimal.abs, Decimal.isPositive, Decimal.isNegative

Checks

Decimal.isZero

Decimal -> Bool

Check if a decimal is zero.

Example:
import Decimal

Decimal.isZero 0d
Try it

See also: Decimal.isPositive, Decimal.isNegative

Decimal.isPositive

Decimal -> Bool

Check if a decimal is positive (greater than zero).

Example:
import Decimal

Decimal.isPositive 5.5d
Try it

Notes: Returns false for zero.

See also: Decimal.isNegative, Decimal.isZero, Decimal.signum

Decimal.isNegative

Decimal -> Bool

Check if a decimal is negative (less than zero).

Example:
import Decimal

Decimal.isNegative (-5.5d)
Try it

Notes: Returns false for zero.

See also: Decimal.isPositive, Decimal.isZero, Decimal.signum

Parsing

Decimal.parse

String -> Result Decimal String

Parse a string to a decimal.

Example:
import Decimal

Decimal.parse "3.14159"
Try it

Notes: Returns Err for invalid decimal strings.

See also: Decimal.toString

Decimal.toString

Decimal -> String

Convert a decimal to its string representation.

Example:
import Decimal

Decimal.toString 3.14d
Try it

See also: Decimal.parse

Inspection

Decimal.scale

Decimal -> Int

Get the number of decimal places (scale) of a decimal.

Example:
import Decimal

Decimal.scale 3.14d
Try it

Notes: Returns the number of digits after the decimal point.

See also: Decimal.normalize

Decimal.normalize

Decimal -> Decimal

Remove trailing zeros from a decimal.

Example:
import Decimal

Decimal.normalize 3.140d
Try it

Notes: Does not change the value, only the representation.

See also: Decimal.scale

Comparison

Decimal.min

Decimal -> Decimal -> Decimal

Returns the smaller of two decimals.

Example:
import Decimal

Decimal.min 3.14d 2.71d
Try it

See also: Decimal.max

Decimal.max

Decimal -> Decimal -> Decimal

Returns the larger of two decimals.

Example:
import Decimal

Decimal.max 3.14d 2.71d
Try it

See also: Decimal.min