Skip to main content

decimal_scaled/types/
quantize.rs

1// SPDX-FileCopyrightText: 2026 John Moxley
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Scale-changing operations for every decimal width.
5//!
6//! Each `Dxx<SCALE>` carries its scale in the type. Converting between
7//! two scales — for instance accumulating cents (`D38<2>`) into a
8//! picometre-precision running total (`D38<12>`) — requires an explicit
9//! quantize.
10//!
11//! Three surfaces, emitted on every width:
12//!
13//! - `quantize::<TARGET>()` — shorthand that uses the crate-default
14//!   rounding mode (`HalfToEven` unless overridden by a `rounding-*`
15//!   Cargo feature). Suitable for the overwhelming majority of cases.
16//! - `quantize_with::<TARGET>(mode)` — takes an explicit
17//!   [`crate::support::rounding::RoundingMode`] for users whose accounting rules
18//!   mandate a non-default rule.
19//! - `with_scale::<TARGET>()` — builder-style alias for `quantize`.
20//!
21//! The 0.5.0 names `rescale` / `rescale_with` remain as deprecated
22//! aliases and are removed in 0.6.0.
23//!
24//! Scale-up direction (target > source) is always exact: the stored
25//! integer is multiplied by `10^diff`. Scale-down direction (target <
26//! source) discards fractional digits using the requested rounding
27//! mode.
28//!
29//! Overflow on the scale-up direction is detected via `checked_mul`
30//! and panics with a clear message in both debug and release builds.
31
32use crate::types::widths::{D18, D38};
33
34// The quantize / quantize_with methods are emitted by
35// `crate::macros::quantize::decl_decimal_quantize!` — same macro for
36// every width; wide tiers receive it from `macros::full`.
37crate::macros::quantize::decl_decimal_quantize!(wide D38, crate::int::types::Int<2>);
38crate::macros::quantize::decl_decimal_quantize!(wide D18, crate::int::types::Int<1>);