#decimal-number #decimal #safe-bindings #decnumber #finance #api-bindings

dec

A decimal arithmetic library providing high-level, safe bindings to libdecnumber

18 releases

0.4.9 Nov 11, 2023
0.4.8 Feb 5, 2022
0.4.7 Jan 15, 2022
0.4.5 Jul 29, 2021
0.3.3 Mar 10, 2021

#51 in Math

Download history 3450/week @ 2023-12-04 3815/week @ 2023-12-11 2978/week @ 2023-12-18 1823/week @ 2023-12-25 2590/week @ 2024-01-01 3625/week @ 2024-01-08 3569/week @ 2024-01-15 4347/week @ 2024-01-22 4041/week @ 2024-01-29 4059/week @ 2024-02-05 5059/week @ 2024-02-12 4526/week @ 2024-02-19 4886/week @ 2024-02-26 4415/week @ 2024-03-04 3651/week @ 2024-03-11 3608/week @ 2024-03-18

16,722 downloads per month
Used in 11 crates (3 directly)

Apache-2.0

1MB
15K SLoC

C 10K SLoC // 0.4% comments Rust 5K SLoC // 0.0% comments

rust-dec

libdecnumber bindings for the Rust programming language.

dec crates.io

A decimal arithmetic library providing high-level, safe bindings to libdecnumber.

# Cargo.toml
[dependencies]
dec = "0.4.9"

View documentation.

decnumber-sys crates.io

Low-level bindings to libdecnumber.

# Cargo.toml
[dependencies]
decnumber-sys = "0.1.5"

View documentation.


lib.rs:

dec is a decimal arithmetic library for Rust.

Introduction

From the Decimal Arithmetic FAQ:

Most people in the world use decimal (base 10) arithmetic. When large or small values are needed, exponents which are powers of ten are used. However, most computers have only binary (base two) arithmetic, and when exponents are used (in floating-poing numbers) they are powers of two.

Binary floating-point numbers can only approximate common decimal numbers. The value 0.1, for example, would need an infinitely recurring binary fraction. In contrast, a decimal number system can represent 0.1 exactly, as one tenth (that is, 10-1). Consequently, binary floating-point cannot be used for financial calculations, or indeed for any calculations where the results achieved are required to match those which might be calculated by hand.

dec is an implementation of the General Decimal Arithmetic standard, which precisely describes both a limited-precision floating-point decimal arithmetic and an arbitrary precision floating-point decimal arithmetic.

The latest draft of the standard is available online at http://speleotrove.com/decimal/decarith.html. The floating-point arithmetic additionally conforms to the IEEE 754-2008 specification, but this specification is not freely available.

Details

dec is a safe Rust API atop the C reference implementation, libdecnumber. Unsafe C bindings to libdecnumber are available in the decnumber-sys crate.

The main types exposed by this library are as follows:

  • Decimal32, a 32-bit decimal floating-point representation which provides 7 decimal digits of precision in a compressed format. This type is intended for storage and interchange only and so does not support any arithmetic functions.

  • Decimal64, a 64-bit decimal floating-point representation which provides 16 decimal digits of precision in a compressed format along with various arithmetic functions.

  • Decimal128, a 128-bit decimal floating-point representation which provides 34 decimal digits of precision in a compressed format along with various arithmetic functions.

  • Decimal, a decimal representation whose precision is configurable via its generic N parameter.

  • Context, which hosts most of the actual functions on the above types. A context configures the behavior of the various operations (e.g., rounding mode) and accumulates exceptional conditions (e.g., overflow).

Examples

The following example demonstrates the basic usage of the library:

use dec::Decimal128;

let x: Decimal128 = ".1".parse()?;
let y: Decimal128 = ".2".parse()?;
let z: Decimal128 = ".3".parse()?;

assert_eq!(x + y, z);
assert_eq!((x + y + z).to_string(), "0.6");

Dependencies