pgnumeric
Arbitrary precision numeric implementation written in Rust, compatible with PostgreSQL's numeric.
See also: PostgreSQL Arbitrary Precision Numbers
This crate provides two types, NumericBuf
and Numeric
.
Optional features
serde
When this optional dependency is enabled, NumericBuf
and Numeric
implement the serde::Serialize
and serde::Deserialize
traits.
Usage
To build a numeric, use NumericBuf
:
use NumericBuf;
let n1: NumericBuf = "123".parse.unwrap;
let n2: NumericBuf = "456".parse.unwrap;
let result = n1 + n2;
assert_eq!;
To build a numeric from Rust primitive types:
use NumericBuf;
let n1: NumericBuf = From from;
let n2: NumericBuf = From from;
let result = n1 + n2;
assert_eq!;
Numeric supports high precision arithmetic operations.
use NumericBuf;
let n1: NumericBuf = "123456789.987654321".parse.unwrap;
let n2: NumericBuf = "987654321.123456789".parse.unwrap;
let result = n1 * n2;
assert_eq!;
let n3 = "170141183460469231731687303715884105727"..unwrap;
assert_eq!
Numeric can be serialized to bytes slice and deserialized from bytes slice.
use ;
let n1 = "123456789.987654321"..unwrap;
let bytes = n1.as_bytes;
let n2 = unsafe ;
assert_eq!;
Rust version requirements
pgnumeric
works on rust 1.43
.
License
This project is licensed under the Apache-2.0 license (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in pgnumeric
by you, shall be licensed as Apache-2.0, without any additional
terms or conditions.