#floating-point #base10 #decimal

dec64-rs

DEC64 decimal floating point with 16 digits of precision

1 unstable release

Uses new Rust 2024

new 0.1.1 Feb 1, 2026
0.1.0 Feb 1, 2026

#1 in #base10

MIT license

74KB
2K SLoC

dec64-rs

DEC64 is a 64-bit decimal floating-point format with 16 digits of precision. This crate provides a Rust implementation that aims to feel idiomatic while staying compatible with the original DEC64 behavior.

Features

  • Base-10 arithmetic with 16 decimal digits of precision.
  • 56-bit coefficient and 8-bit exponent (value = coefficient * 10^exponent).
  • NaN payloads reserved for TRUE, FALSE, and NULL (per DEC64 spec).
  • checked_* operations that return explicit Dec64Error variants.
  • Formatting control via Dec64Formatter.

Quick start

use dec64_rs::Dec64;

let a: Dec64 = "0.1".parse().unwrap();
let b: Dec64 = "0.2".parse().unwrap();
assert_eq!(a + b, "0.3".parse().unwrap());

let x = Dec64::from(123);
let y = Dec64::new(456, -2); // 4.56
assert_eq!((x + y).to_string(), "127.56");

Precision and range

  • Coefficient range: -36,028,797,018,963,968 to 36,028,797,018,963,967.
  • Exponent range: -127 to 127.
  • Effective precision: 16 decimal digits.

Conversions

Integer conversions are infallible with From. Floating-point conversions use TryFrom<f64> and return explicit errors if the input is NaN/infinite or cannot be represented without loss:

use dec64_rs::{Dec64, Dec64Error};

let ok = Dec64::try_from(1.25_f64);
assert!(ok.is_ok());

let bad = Dec64::try_from(f64::INFINITY);
assert_eq!(bad, Err(Dec64Error::NonFiniteFloat));

Checked math

Use checked_* methods when you want explicit domain/NaN errors:

use dec64_rs::{Dec64, Dec64Error};

let x = Dec64::NEGATIVE_ONE;
assert_eq!(x.checked_sqrt(), Err(Dec64Error::Domain));

Formatting

use dec64_rs::{Dec64, Dec64Formatter};

let n = Dec64::new(1234567, -2); // 12345.67
let f = Dec64Formatter::new().separator(Some(',')).places(4);
assert_eq!(f.format(n), "12,345.6700");

Dec64Formatter renders NaN as "NaN" (the C reference uses an empty string).

License

MIT

No runtime deps