On this page
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 42Notes: Always succeeds. Preserves exact integer value. This is the route from an Int value to a Decimal: an Int value never converts by itself, so with quantity = 3, 19.99d * quantity is a type error and 19.99d * Decimal.fromInt quantity is 59.97d. An integer literal written beside a Decimal is built as one: 19.99d * 3 is 59.97d.
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.14Notes: Returns Err for NaN, infinity, or values outside decimal range.
See also: Decimal.fromInt, Decimal.toFloat
Decimal.toInt
Decimal -> Maybe Int
Convert a decimal to an integer by truncating towards zero. Returns Nothing if the value is too large for Int.
import Decimal
import Maybe
Decimal.toInt 3.7d |> Maybe.withDefault 0Notes: Truncates fractional part. Returns Nothing when the value exceeds Int range. Use round/floor/ceil first for different behavior.
See also: Decimal.toFloat, Decimal.truncate
Decimal.toFloat
Decimal -> Maybe Float
Convert a decimal to a float. Returns Nothing if the conversion fails.
import Decimal
import Maybe
Decimal.toFloat 3.14d |> Maybe.withDefault 0.0Notes: May lose precision for large or high-precision decimals.
See also: Decimal.toInt, Decimal.fromFloat
Rounding
Decimal.round
Int -> Decimal -> Maybe Decimal
Round a decimal to n decimal places using banker's rounding. Returns Nothing if places is outside 0..=28.
import Decimal
import Maybe
Decimal.round 2 3.14159d |> Maybe.withDefault 0.0dNotes: Uses banker's rounding (round half to even). Places must be in 0..=28; returns Nothing otherwise.
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.7dNotes: 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.2dNotes: 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.7dNotes: 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)See also: Decimal.sign, Decimal.isNegative
Decimal.sign
Decimal -> Int
Returns the sign of a decimal: -1, 0, or 1.
import Decimal
Decimal.sign (-5.5d)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.
import Decimal
Decimal.isZero 0dSee also: Decimal.isPositive, Decimal.isNegative
Decimal.isPositive
Decimal -> Bool
Check if a decimal is positive (greater than zero).
import Decimal
Decimal.isPositive 5.5dNotes: Returns false for zero.
See also: Decimal.isNegative, Decimal.isZero, Decimal.sign
Decimal.isNegative
Decimal -> Bool
Check if a decimal is negative (less than zero).
import Decimal
Decimal.isNegative (-5.5d)Notes: Returns false for zero.
See also: Decimal.isPositive, Decimal.isZero, Decimal.sign
Parsing
Decimal.parse
String -> Result Decimal String
Parse a string to a decimal.
import Decimal
Decimal.parse "3.14159"Notes: 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.14dSee also: Decimal.parse
Inspection
Decimal.scale
Decimal -> Int
Get the number of decimal places (scale) of a decimal.
import Decimal
Decimal.scale 3.14dNotes: 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.140dNotes: 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.71dSee also: Decimal.max
Decimal.max
Decimal -> Decimal -> Decimal
Returns the larger of two decimals.
import Decimal
Decimal.max 3.14d 2.71dSee also: Decimal.min