API Reference

Types

DataDecimals.AbstractDecimal — Type
DataDecimals.AbstractDecimal <: Real

Supertype of the package's exact decimal types, Decimal (scale fixed in the type) and DataDecimals.DecimalValue (scale carried per value). Every AbstractDecimal is an exact rational of the form unscaled(x) * 10^-scale(x); there are no NaN or Inf values, so isfinite is always true and isnan always false.

source
DataDecimals.Decimal — Type
Decimal{P, S, T} <: DataDecimals.AbstractDecimal
Decimal{P, S}

An exact fixed-scale decimal: the value is unscaled * 10^-S, with the invariant |unscaled| < 10^P. P is the precision (total decimal digits), S the scale (fractional digits, 0 <= S <= P), and T the storage integer — Int32, Int64, Int128, or DataDecimals.Int256 for P up to 9, 18, 38, or 76 respectively. Writing Decimal{P,S} fills T in from P.

The struct holds nothing but the storage integer, so Vector{Decimal{P,S,T}} can share an Arrow coefficient buffer of the same width and host byte order. Other wire formats require their own encoding. See DataDecimals.Decimal64 and its siblings for the aliases naming those tiers.

Constructors are exact or they throw: Decimal{18,2}(5), Decimal{18,2}(1//4), and convert raise InexactError/OverflowError rather than round. The exceptions are construction from AbstractFloat, which rounds half-even at scale S like every cross-representation float conversion in Base, and construction from a string, which is parse (also half-even). DataDecimals.rescale and round(D, x, mode) are the explicitly-rounding forms.

reinterpret(Decimal{P,S,T}, u) builds a value straight from an unscaled coefficient without checking the |u| < 10^P invariant — the zero-copy wire decode path, where the invariant is the caller's contract.

julia> Decimal{18,2}("1234.56")
1234.56

julia> Decimal{18,2}(5) === reinterpret(Decimal{18,2,Int64}, 500)
true

Values display as plain digits; repr/show give the round-trippable typed form Decimal{18,2,Int64}("1234.56").

source
DataDecimals.DecimalValue — Type
DataDecimals.DecimalValue{T} <: DataDecimals.AbstractDecimal
DataDecimals.DecimalValue(unscaled, scale)

An exact decimal whose scale travels with the value instead of with the type: unscaled * 10^-scale, where scale is an Int32 in 0:16383. T is the storage integer (Int32, Int64, Int128, or DataDecimals.Int256); the bare DataDecimals.DecimalValue(u, s) constructor infers it from u, defaulting to Int64.

This is the type for row-oriented wire values that carry their own scale — a PostgreSQL numeric's per-value dscale, or the output of normalize — and for values whose scale is not known until runtime. It is still isbits, but it is one word wider than Decimal and its arithmetic aligns scales at run time rather than at compile time, so prefer Decimal{P,S} for columnar data.

Like Decimal, conversions in are exact-or-throw except from AbstractFloat, where DataDecimals.DecimalValue(x) produces the exact binary expansion of x (that is what a runtime scale is for) rather than rounding to a fixed scale. DataDecimals.rescale moves a value to another scale.

julia> DataDecimals.DecimalValue(12345, 3)
12.345

julia> repr(DataDecimals.DecimalValue(0.1))
"DataDecimals.DecimalValue{DataDecimals.Int256}(\"0.1000000000000000055511151231257827021181583404541015625\")"
source
DataDecimals.Decimal64 — Type
DataDecimals.Decimal64{S}
DataDecimals.Decimal32{S}
DataDecimals.Decimal128{S}
DataDecimals.Decimal256{S}

Aliases for the four storage tiers at their maximum precision:

aliasexpands todigitsbits
DataDecimals.Decimal32{S}Decimal{9,S,Int32}932
DataDecimals.Decimal64{S}Decimal{18,S,Int64}1864
DataDecimals.Decimal128{S}Decimal{38,S,Int128}38128
DataDecimals.Decimal256{S}Decimal{76,S,DataDecimals.Int256}76256

S is the scale, so DataDecimals.Decimal64{2} is the usual money type. Use Decimal{P,S} directly when a schema pins a precision narrower than the tier's maximum.

julia> DataDecimals.Decimal64{2} === Decimal{18,2,Int64}
true
source

Literals

DataDecimals.@dec_str — Macro
@dec_str -> Decimal
dec"1.25"

An exact decimal literal. Produces the minimal-fitting Decimal{P,S} (so dec"1.25" isa Decimal{3,2,Int32}), constant-folded at parse time; literals beyond 76 digits of precision fall back to a DataDecimals.DecimalValue{Int256}. Digit groups may be separated with underscores (dec"1_000_000.25"). The literal must be exactly representable — no silent rounding.

Exponents are accepted and folded into the scale (dec"1.5e-3" is Decimal{4,4,Int32}), and trailing zeros are significant: dec"1.50" has scale 2 while dec"1.5" has scale 1 (they compare equal). A literal that cannot be represented exactly — more than 77 significant digits, or a magnitude past the widest tier — is an ArgumentError at parse time, not a rounded value.

Because the type is minimal-fitting, a literal used in arithmetic promotes to whatever the other operand needs; annotate explicitly (DataDecimals.Decimal64{2}("1.25"), or convert) when a column type is wanted.

source

Rounding and rescaling

DataDecimals.rescale — Function
DataDecimals.rescale(D::Type{<:Decimal}, x, mode=RoundNearest) -> D
DataDecimals.rescale(x::DataDecimals.DecimalValue, s::Integer, mode=RoundNearest) -> DataDecimals.DecimalValue

Move a decimal to another scale, rounding with mode when the target scale cannot hold x exactly. The first form targets a Decimal type D (whose scale is a type parameter); the second returns a DataDecimals.DecimalValue at the runtime scale s, which must be in 0:16383.

mode is any of RoundNearest (half-even, the default), RoundNearestTiesAway, RoundNearestTiesUp, RoundToZero, RoundFromZero, RoundDown, RoundUp, or DataDecimals.RoundExact, which throws InexactError instead of dropping a digit. Rounding up in scale is always exact, so mode only matters when digits are dropped; overflowing the target's precision still throws OverflowError.

convert/constructors are the exact-or-throw counterpart, and round(D, x, mode) is the same operation spelled Base's way.

julia> DataDecimals.rescale(DataDecimals.Decimal64{2}, DataDecimals.Decimal64{4}("1.2356"), RoundUp)
1.24

julia> DataDecimals.rescale(DataDecimals.DecimalValue(12345, 3), 1)
12.3
source
DataDecimals.divide — Function
DataDecimals.divide(x, y, mode=RoundNearest)

Divide two decimals, rounding the true quotient at scale max(scale(x), scale(y)) with mode. x / y is exactly DataDecimals.divide(x, y, RoundNearest) (half-even) — there is no global rounding state, so a different mode is always an argument, never a context.

mode is any of RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp, RoundToZero, RoundFromZero, RoundDown, RoundUp. Dividing by zero throws DivideError; a quotient too large for the result type throws OverflowError. For Decimal operands the result type is derived from the operand parameters (the widest quotient that can occur, capped at the 76-digit tier); for DataDecimals.DecimalValue operands it keeps the promoted storage type and the runtime scale max(scale(x), scale(y)).

julia> DataDecimals.divide(DataDecimals.Decimal64{2}("1.00"), DataDecimals.Decimal64{2}("3.00"))
0.33

julia> DataDecimals.divide(DataDecimals.Decimal64{2}("1.00"), DataDecimals.Decimal64{2}("3.00"), RoundUp)
0.34
source
DataDecimals.normalize — Function
normalize(x) -> DataDecimals.DecimalValue

The value of x with all trailing zeros removed from the coefficient and the scale reduced to match (scales never go below zero, so integer values keep scale 0): normalize(dec"1.2000") == DataDecimals.DecimalValue(12, 1).

The result is always a DataDecimals.DecimalValue — the scale is a property of the value, not of the type — and keeps the input's storage type. The numeric value is unchanged (normalize(x) == x always), so this only matters where the representation is observable: the digits string prints, or a wire format that transmits the scale.

source
DataDecimals.RoundExact — Constant
DataDecimals.RoundExact

A rounding mode that requires the operation to be exact. Wherever a mode is taken, RoundExact never rounds: DataDecimals.rescale, round(D, x, RoundExact), round(I, x, RoundExact), round(x, RoundExact; digits), DataDecimals.divide, and div, rem, and divrem with a mode throw InexactError when a digit would be dropped. In the Parsers extension, Parsers.parse throws InexactError for a value the target scale cannot hold exactly, Parsers.tryparse returns nothing, and Parsers.parsenext returns Parsers.RC_INVALID. Scaling up is always exact, so trailing zeros never fail.

julia> DataDecimals.rescale(DataDecimals.Decimal64{2}, DataDecimals.Decimal64{4}("1.2300"), DataDecimals.RoundExact)
1.23

julia> DataDecimals.rescale(DataDecimals.Decimal64{2}, DataDecimals.Decimal64{4}("1.2345"), DataDecimals.RoundExact)
ERROR: InexactError: convert(Decimal64{2}, Decimal{18,4,Int64}("1.2345"))
[...]
source

Wire and byte-level API

DataDecimals.unscaled — Function
DataDecimals.unscaled(x) -> Integer

The unscaled coefficient of a decimal: x == unscaled(x) * 10^-scale(x). The returned integer is the value's storage type, and is exactly the coefficient an Arrow/Parquet/DuckDB buffer stores. reinterpret(typeof(x), unscaled(x)) rebuilds x.

source
DataDecimals.scale — Function
DataDecimals.scale(x) -> Int
DataDecimals.scale(::Type{<:Decimal}) -> Int

The scale (count of fractional decimal digits) of a decimal value: the S parameter for a Decimal, the stored per-value scale for a DataDecimals.DecimalValue. The companion precision is Base.precision, which is defined for Decimal types only. scale accepts a Decimal type as well as a value, since the scale is a type parameter there.

source
DataDecimals.decimallength — Function
DataDecimals.decimallength(x) -> Int

Number of bytes writedecimal! produces for x (sign, digits, and decimal point included) — equivalently ncodeunits(string(x)). Computed from the coefficient's digit count and the scale, without formatting anything, so it is the size to reserve before writing.

source
DataDecimals.writedecimal! — Function
DataDecimals.writedecimal!(buf, pos, x) -> Int

Write the plain decimal form of x into the byte buffer buf starting at pos; returns the first position after the written bytes. The caller must ensure decimallength(x) bytes of room — nothing is bounds-checked or grown.

The output is always the positional form (-1234.56, 0.0005), never scientific notation, so it is directly usable as a MySQL parameter, a CSV field, or a JSON number. This is the allocation-free counterpart to string(x), which is implemented on top of it.

julia> buf = zeros(UInt8, 16);

julia> n = DataDecimals.writedecimal!(buf, 1, DataDecimals.Decimal64{2}("-1.25"))
6

julia> String(buf[1:n-1])
"-1.25"
source

Base methods

These Base functions have methods for the decimal types; they are documented by Base and behave as described in Semantics.

Construction and conversion : convert, reinterpret, parse and tryparse (via the Parsers extension), promote_rule, Float64 / Float32 / Float16 / BigFloat / AbstractFloat, Rational, Integer and the concrete integer types, big, rationalize, widen.

Arithmetic : +, -, *, /, div, rem, mod, fld, cld, divrem, sum, abs, Base.Checked.checked_add / checked_sub / checked_mul / checked_neg / checked_abs.

Rounding : round, trunc, floor, ceil, in both the in-type forms (with a digits keyword) and the converting forms round(T, x, mode).

Comparison and hashing : ==, <, <=, isless, Base.decompose, hash, signbit, iszero, isinteger, isfinite, isnan, isinf.

Type traits and constants : zero, one, eps, typemin, typemax, floatmin, floatmax, precision.

Text : string, print, show.

Other : Random.rand (uniform on [0, 1) at the type's scale), and broadcast kernels for elementwise +, -, * over vectors.

Extension entry points

Available when the corresponding package is loaded; see Extensions in the manual.

Parsers.parse, Parsers.tryparse, Parsers.parsenext : byte-level parsing with decimal= and rounding= keywords (Parsers 3).

Printf.tofloat, Printf.toint : exact %f / %e / %g and exact-or-InexactError %d.

JSON.tostring : JSON numbers carrying the exact digits (JSON 1.x).

LinearAlgebra factorizations : lu, cholesky, qr, svd, eigen, hessenberg, schur, lq, det, logdet, inv, eigvals, svdvals, cond, nullspace, pinv, rank route through float(A).

Index