ddn.num.fixed_decimal

ddn.num.fixed_decimal — bounded fixed-point decimal values.

FixedDecimal!(Storage, Scale) represents exact decimal values as coefficient × 10⁻ˢᶜᵃˡᵉ, where the developer picks the underlying integer storage and the decimal-digit precision is derived from it:

  • byte — 2 digits, short — 4, int — 9, long — 18.
  • std.int128.Int128 — 38 digits (the cent-shaped class).
Scale is the number of fraction digits: -1 (the default) selects the runtime-scale form (a stored ubyte in 0..precision); any other value fixes the scale at compile time and stores no scale field. Values are exact — every conversion is checked and inexact operations throw rather than round (rounding arrives with the arithmetic layer, under an explicit policy).

This is fixed-point, not IEEE 754 decimal floating point: no per-value exponents, no NaN/Inf, no rounding modes. Type.DECIMAL in ddn.var is reserved for that different family.

Money is a template alias of FixedDecimal for financial spelling — the same types, no currency identity and no default scale.

The type is standalone: it depends on Phobos only (never ddn.var, formats or encodings), and all operations are CTFE-safe — compile-time constant decimals work from day one.

Design record: doc/DDN-FIXED-DECIMAL.md.

Example:

import ddn.num.fixed_decimal;

auto price = FixedDecimal!long.fromString("19.99");
assert(price.precision == 18);
assert(price.toString() == "19.99");

auto rate = Money!(long, 4).fromString("0.0750");
assert(rate.scale == 4);

enum oneFive = FixedDecimal!long.fromString("1.5"); // CTFE works
static assert(oneFive.toString() == "1.5");

Types 4

classFixedDecimalException : Exception

Thrown for rejected fixed-decimal operations: malformed parsing, precision overflow and inexact conversions.

Constructors
this(string msg)Params: msg = description of the rejected operation.
structFixedDecimal(I, int Scale = - 1) if (is(I == byte) || is(I == short) || is(I == int) || is(I == long) || is(I == Int128))

Bounded fixed-point decimal: value = coefficient × 10⁻ˢᶜᵃˡᵉ.

Parameters

Storageunderlying integer storage: byte, short, int, long or std.int128.Int128. The decimal-digit precision is derived from it (precisionOf!Storage).
Scalefraction digit count; -1 (default) selects the runtime-scale form, 0..precisionOf!Storage fixes the scale at compile time.

See Also

Money for the financial-domain alias of the same template.
Fields
int precisionDerived decimal-digit precision of this type.
int scaleTemplateCompile-time scale; `-1` for the runtime-scale form.
Methods
ubyte scale() @property const @safe pure nothrow @nogcThe scale (fraction digit count): the template constant for fixed-scale forms, the stored byte for runtime-scale ones.
I unscaled() @property const @safe pure nothrow @nogcThe unscaled two's-complement coefficient (the raw storage integer; the column layer reads it back after arithmetic).
FixedDecimal fromUnscaled(long coefficient, ubyte scale) static @safe pureBuilds a value from unscaled integer parts.
FixedDecimal fromString(scope const(char)[] text) static @safe pureParses an exact decimal literal.
string toString() const @safe pureFormats the value in exact decimal notation.
auto atScale(int S2)() if (S2 >= 0 && S2 <= precisionOf!I) const @safe pureConverts to a fixed-scale type (checked).
auto asRuntime() const @safe pure nothrow @nogcThe same value as a runtime-scale instance.
FixedDecimal opBinary(string op)(const FixedDecimal rhs) if (op == "+" || op == "-") const @safe pure`+`/`-` between values of the same instantiation: the result is that same instantiation (fixed-scale operands share their scale, so alignment is identity).
auto opBinary(string op, int S2)(const FixedDecimal!(I, S2) rhs) if ((op == "+" || op == "-") && S2 != Scale) const @safe pure`+`/`-` between fixed-scale forms of different compile-time scale (same storage): operands convert to the runtime form; the result is the runtime form at the aligned scale.
auto opBinary(string op, J2, int S2)(const FixedDecimal!(J2, S2) rhs) if ((op == "+" || op == "-") && !is(J2 == I)) const @safe pure`+`/`-` across storage classes: both operands widen to the `Int128` runtime form (exact); the result is the wide runtime form.
auto opBinary(string op)(const long rhs) if (op == "+" || op == "-") const @safe pure`+`/`-` with a signed integer operand (exact conversion or throw).
auto opBinaryRight(string op)(const long lhs) if (op == "+" || op == "-") const @safe pureDitto (integer on the left).
FixedDecimal opUnary(string op)() if (op == "-") const @safe pure nothrow @nogcNegation (sign flip via the magnitude path, so negating the largest negative coefficient cannot overflow).
FixedDecimal abs() const @safe pure nothrow @nogcThe absolute value (same scale/form).
auto opBinary(string op)(const FixedDecimal rhs) if (op == "*") const @safe pure`*` between values of the same instantiation. Fixed-scale operands yield the fixed-scale type of the summed scale (`S + S`, compile-time checked); runtime-scale operands yield the runtime form.
auto opBinary(string op, int S2)(const FixedDecimal!(I, S2) rhs) if (op == "*" && S2 != Scale) const @safe pure`*` between different scale forms of the same storage: fixed × fixed yields `FixedDecimal!(I, S1 + S2)` (compile-time checked); any runtime-scale operand yields the runtime form.
auto opBinary(string op, J2, int S2)(const FixedDecimal!(J2, S2) rhs) if (op == "*" && !is(J2 == I)) const @safe pure`*` across storage classes: both operands widen to the `Int128` runtime form; the result is the wide runtime form.
auto opBinary(string op)(const long rhs) if (op == "*") const @safe pure`*` with a signed integer operand (exact conversion or throw).
auto opBinaryRight(string op)(const long lhs) if (op == "*") const @safe pureDitto (integer on the left).
auto opBinary(string op)(const FixedDecimal rhs) if (op == "/") const @safe pureDivision: result scale is `max(s1, s2)` rounded half-even (convenience form, D4). Precision-critical callers use divideToScale. The result is the runtime-scale form.
auto opBinary(string op, int S2)(const FixedDecimal!(I, S2) rhs) if (op == "/" && S2 != Scale) const @safe pureDitto, different scale forms of the same storage.
auto opBinary(string op, J2, int S2)(const FixedDecimal!(J2, S2) rhs) if (op == "/" && !is(J2 == I)) const @safe pureDitto, across storage classes (widen both; wide runtime result).
auto opBinary(string op)(const long rhs) if (op == "/") const @safe pure`/` by a signed integer (exact conversion or throw).
auto opBinaryRight(string op)(const long lhs) if (op == "/") const @safe pureDitto (integer divided by a decimal).
auto divideToScale(int S2)(const FixedDecimal!(I, S2) rhs, ubyte resultScale) const @safe pureDivision with an explicit result scale, rounded half-even.
bool opEquals(ref const typeof(this) rhs) const @safe pureStructural-value equality (numeric, across scales and forms).
bool opEquals(FD)(auto ref const FD rhs) if (_isFD!FD && !is(FD == typeof(this))) const @safe pureMixed-type equality: any `FixedDecimal` instantiation compares by value.
int opCmp(ref const typeof(this) rhs) const @safe pureNumeric ordering (total, across scales and forms).
int opCmp(FD)(auto ref const FD rhs) if (_isFD!FD && !is(FD == typeof(this))) const @safe pureMixed-type ordering: any `FixedDecimal` instantiation.
size_t toHash() const @safe pureHash consistent with value equality: equal values hash equal across scales, capacities and scale forms (canonical form, trailing fraction zeros stripped).

Financial-domain alias of the whole FixedDecimal template: both signatures, identical semantics — no currency identity, no default scale of 2, not a distinct nominal type.

Convenience alias for the runtime-scale Int128-backed (38-digit) specialization.

Templates 3

tmplprecisionOf(I)

Derived full decimal-digit precision of a storage type: the largest d with 10^d − 1 representable as a positive magnitude.

Parameters

Ione of the supported signed storage types.

Returns

The precision in decimal digits (byte → 2, short → 4, int → 9,

long → 18, Int128 → 38).

tmpl_precisionMax(int precision)

Compile-time 10^precision − 1 as a big-endian magnitude.

tmpl_isFD(T)

True when T is any FixedDecimal instantiation.