pub struct Decimal64NoScale { /* private fields */ }Expand description
A fixed-precision decimal stored as a raw 64-bit integer.
§Design
Unlike Decimal64 which embeds scale, Decimal64NoScale stores only the
mantissa. This provides:
- 2 more digits of precision (18 vs 16)
- Correct aggregates (sum/min/max work with raw integer math)
- Columnar storage compatibility (scale in metadata, not per-value)
§Aggregates
stored_values = [a*10^s, b*10^s, c*10^s]
SUM(stored_values) = (a+b+c) * 10^s
actual_sum = SUM / 10^s = a+b+c ✓§Special Values
Special values use sentinel i64 values (at the extremes of the range):
i64::MIN: NaN (sorts highest per PostgreSQL semantics)i64::MIN + 1: -Infinity (sorts lowest)i64::MAX: +Infinity
Implementations§
Source§impl Decimal64NoScale
impl Decimal64NoScale
Sourcepub fn new(s: &str, scale: i32) -> Result<Self, DecimalError>
pub fn new(s: &str, scale: i32) -> Result<Self, DecimalError>
Creates a Decimal64NoScale from a string with the given scale.
§Arguments
s- The decimal string (e.g., “123.45”)scale- The number of decimal places to store
§Examples
use decimal_bytes::Decimal64NoScale;
let d = Decimal64NoScale::new("123.45", 2).unwrap();
assert_eq!(d.value(), 12345);
assert_eq!(d.to_string_with_scale(2), "123.45");Sourcepub const fn from_raw(value: i64) -> Self
pub const fn from_raw(value: i64) -> Self
Creates a Decimal64NoScale from a raw i64 value.
Use this when you already have the scaled integer value.
§Examples
use decimal_bytes::Decimal64NoScale;
let d = Decimal64NoScale::from_raw(12345);
assert_eq!(d.value(), 12345);
assert_eq!(d.to_string_with_scale(2), "123.45");Sourcepub const fn neg_infinity() -> Self
pub const fn neg_infinity() -> Self
Creates negative infinity.
Sourcepub const fn nan() -> Self
pub const fn nan() -> Self
Creates NaN (Not a Number).
Follows PostgreSQL semantics: NaN == NaN is true.
Sourcepub const fn value(&self) -> i64
pub const fn value(&self) -> i64
Returns the raw i64 value.
For normal values, this is actual_value * 10^scale.
For special values, this returns the sentinel value.
Sourcepub const fn raw(&self) -> i64
pub const fn raw(&self) -> i64
Returns the raw i64 value (alias for columnar storage compatibility).
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if this value is negative (excluding special values).
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if this value is positive (excluding special values).
Sourcepub fn is_pos_infinity(&self) -> bool
pub fn is_pos_infinity(&self) -> bool
Returns true if this value is positive infinity.
Sourcepub fn is_neg_infinity(&self) -> bool
pub fn is_neg_infinity(&self) -> bool
Returns true if this value is negative infinity.
Sourcepub fn is_infinity(&self) -> bool
pub fn is_infinity(&self) -> bool
Returns true if this value is positive or negative infinity.
Sourcepub fn is_special(&self) -> bool
pub fn is_special(&self) -> bool
Returns true if this is a special value (Infinity or NaN).
Sourcepub fn to_string_with_scale(&self, scale: i32) -> String
pub fn to_string_with_scale(&self, scale: i32) -> String
Formats the value as a decimal string using the given scale.
§Arguments
scale- The scale to use for formatting
§Examples
use decimal_bytes::Decimal64NoScale;
let d = Decimal64NoScale::from_raw(12345);
assert_eq!(d.to_string_with_scale(2), "123.45");
assert_eq!(d.to_string_with_scale(3), "12.345");
assert_eq!(d.to_string_with_scale(0), "12345");Sourcepub fn to_be_bytes(&self) -> [u8; 8]
pub fn to_be_bytes(&self) -> [u8; 8]
Returns the 8-byte big-endian representation.
Sourcepub fn from_be_bytes(bytes: [u8; 8]) -> Self
pub fn from_be_bytes(bytes: [u8; 8]) -> Self
Creates a Decimal64NoScale from big-endian bytes.
Sourcepub fn to_decimal(&self, scale: i32) -> Decimal
pub fn to_decimal(&self, scale: i32) -> Decimal
Converts to the variable-length Decimal type.
Note: This requires a scale to format correctly.
Sourcepub fn from_decimal(decimal: &Decimal, scale: i32) -> Result<Self, DecimalError>
pub fn from_decimal(decimal: &Decimal, scale: i32) -> Result<Self, DecimalError>
Creates a Decimal64NoScale from a Decimal with the specified scale.
Sourcepub fn cmp_with_scale(
&self,
other: &Self,
self_scale: i32,
other_scale: i32,
) -> Ordering
pub fn cmp_with_scale( &self, other: &Self, self_scale: i32, other_scale: i32, ) -> Ordering
Compares two values, normalizing scales if different.
This is needed when comparing values that might have been stored with different scales in different columns.
Trait Implementations§
Source§impl Clone for Decimal64NoScale
impl Clone for Decimal64NoScale
Source§fn clone(&self) -> Decimal64NoScale
fn clone(&self) -> Decimal64NoScale
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Decimal64NoScale
impl Debug for Decimal64NoScale
Source§impl Default for Decimal64NoScale
impl Default for Decimal64NoScale
Source§fn default() -> Decimal64NoScale
fn default() -> Decimal64NoScale
Source§impl<'de> Deserialize<'de> for Decimal64NoScale
impl<'de> Deserialize<'de> for Decimal64NoScale
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for Decimal64NoScale
impl Display for Decimal64NoScale
Source§impl From<i32> for Decimal64NoScale
impl From<i32> for Decimal64NoScale
Source§impl From<i64> for Decimal64NoScale
impl From<i64> for Decimal64NoScale
Source§impl Hash for Decimal64NoScale
impl Hash for Decimal64NoScale
Source§impl Ord for Decimal64NoScale
impl Ord for Decimal64NoScale
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compares values assuming same scale.
For cross-scale comparison, use cmp_with_scale().