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.
import Decimal
Decimal.fromInt 42Try itNotes: 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).
import Decimal
Decimal.fromFloat 3.14Try itNotes: 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.
import Decimal
Decimal.toInt 3.7dTry itNotes: 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.
import Decimal
Decimal.toFloat 3.14dTry itNotes: 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.
import Decimal
Decimal.round 2 3.14159dTry itNotes: 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.
import Decimal
Decimal.floor 3.7dTry itNotes: 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.
import Decimal
Decimal.ceil 3.2dTry itNotes: 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).
import Decimal
Decimal.truncate 3.7dTry itNotes: 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.
import Decimal
Decimal.abs (-5.5d)Try itSee also: Decimal.signum, Decimal.isNegative
Decimal.signum
Decimal -> Int
Returns the sign of a decimal: -1, 0, or 1.
import Decimal
Decimal.signum (-5.5d)Try itNotes: 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.
import Decimal
Decimal.isZero 0dTry itSee also: Decimal.isPositive, Decimal.isNegative
Decimal.isPositive
Decimal -> Bool
Check if a decimal is positive (greater than zero).
import Decimal
Decimal.isPositive 5.5dTry itNotes: 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).
import Decimal
Decimal.isNegative (-5.5d)Try itNotes: 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.
import Decimal
Decimal.parse "3.14159"Try itNotes: Returns Err for invalid decimal strings.
See also: Decimal.toString
Decimal.toString
Decimal -> String
Convert a decimal to its string representation.
import Decimal
Decimal.toString 3.14dTry itSee also: Decimal.parse
Inspection
Decimal.scale
Decimal -> Int
Get the number of decimal places (scale) of a decimal.
import Decimal
Decimal.scale 3.14dTry itNotes: Returns the number of digits after the decimal point.
See also: Decimal.normalize
Decimal.normalize
Decimal -> Decimal
Remove trailing zeros from a decimal.
import Decimal
Decimal.normalize 3.140dTry itNotes: Does not change the value, only the representation.
See also: Decimal.scale
Comparison
Decimal.min
Decimal -> Decimal -> Decimal
Returns the smaller of two decimals.
import Decimal
Decimal.min 3.14d 2.71dTry itSee also: Decimal.max
Decimal.max
Decimal -> Decimal -> Decimal
Returns the larger of two decimals.
import Decimal
Decimal.max 3.14d 2.71dTry itSee also: Decimal.min