High precision decimal

Overview

decimal-rs

Apache-2.0 licensed Crate API

High precision decimal with maximum precision of 38.

Optional features

serde

When this optional dependency is enabled, Decimal implements the serde::Serialize and serde::Deserialize traits.

Usage

To build a decimal, use Decimal:

use decimal_rs::Decimal;

let n1: Decimal = "123".parse().unwrap();
let n2: Decimal = "456".parse().unwrap();
let result = n1 + n2;
assert_eq!(result.to_string(), "579");

To build a decimal from Rust primitive types:

use decimal_rs::Decimal;

let n1 = Decimal::from(123_i32);
let n2 = Decimal::from(456_i32);
let result = n1 + n2;
assert_eq!(result, Decimal::from(579_i32));

Decimal supports high precision arithmetic operations.

use decimal_rs::Decimal;

let n1: Decimal = "123456789.987654321".parse().unwrap();
let n2: Decimal = "987654321.123456789".parse().unwrap();
let result = n1 * n2;
assert_eq!(result.to_string(), "121932632103337905.662094193112635269");

Decimal can be encoded to bytes and decoded from bytes.

use decimal_rs::Decimal;

let n1 = "123456789.987654321".parse::<Decimal>().unwrap();
let mut  bytes = Vec::new();
n1.encode(&mut bytes).unwrap();
let n2 = Decimal::decode(&bytes);
assert_eq!(n1, n2);

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 decimal-rs by you, shall be licensed as Apache-2.0, without any additional terms or conditions.

You might also like...
A high performance blockchain kernel for enterprise users.
A high performance blockchain kernel for enterprise users.

English | ็ฎ€ไฝ“ไธญๆ–‡ What is CITA CITA is a fast and scalable blockchain kernel for enterprises. CITA supports both native contract and EVM contract, by whi

High performance and distributed KV store w/ REST API. ๐Ÿฆ€
High performance and distributed KV store w/ REST API. ๐Ÿฆ€

About Lucid KV High performance and distributed KV store w/ REST API. ๐Ÿฆ€ Introduction Lucid is an high performance, secure and distributed key-value s

A high-performance observability data pipeline.

Get Started โ€ข Docs โ€ข Guides โ€ข Integrations โ€ข Chat โ€ข Download What is Vector? Vector is a high-performance, end-to-end (agent & aggregator) observabili

Read input lines as byte slices for high efficiency

bytelines This library provides an easy way to read in input lines as byte slices for high efficiency. It's basically lines from the standard library,

Zero-cost high-level lua 5.3 wrapper for Rust

td_rlua This library is a high-level binding for Lua 5.3. You don't have access to the Lua stack, all you can do is read/write variables (including ca

High-level Rust bindings to Perl XS API

Perl XS for Rust High-level Rust bindings to Perl XS API. Example xs! { package Array::Sum; sub sum_array(ctx, array: AV) { array.iter().map(|

Facilitating high-level interactions between Wasm modules and JavaScript

wasm-bindgen Facilitating high-level interactions between Wasm modules and JavaScript. Guide | API Docs | Contributing | Chat Built with ๐Ÿฆ€ ๐Ÿ•ธ by The

High-level PortMidi bindings and wrappers for Rust

portmidi-rs High-level PortMidi bindings for Rust. PortMidi website: http://portmedia.sourceforge.net/portmidi/ Installation Add this to your Cargo.to

High-performance runtime for data analytics applications

Weld Documentation Weld is a language and runtime for improving the performance of data-intensive applications. It optimizes across libraries and func

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely ๐Ÿฆ€ ๐Ÿ“ˆ๐Ÿš€
A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely ๐Ÿฆ€ ๐Ÿ“ˆ๐Ÿš€

Plotters - A Rust drawing library focus on data plotting for both WASM and native applications ๐Ÿฆ€ ๐Ÿ“ˆ ๐Ÿš€ Plotters is drawing library designed for rende

Rust high performance xml reader and writer

quick-xml High performance xml pull reader/writer. The reader: is almost zero-copy (use of Cow whenever possible) is easy on memory allocation (the AP

High-performance browser-grade HTML5 parser

html5ever API Documentation html5ever is an HTML parser developed as part of the Servo project. It can parse and serialize HTML according to the WHATW

High level FFI binding around the sys mount & umount2 calls, for Rust

sys-mount High level FFI bindings to the mount and umount2 system calls, for Rust. Examples Mount This is how the mount command could be written with

High performance Rust ECS library
High performance Rust ECS library

Legion aims to be a feature rich high performance Entity component system (ECS) library for Rust game projects with minimal boilerplate. Getting Start

High-performance log search engine.

NOTE: This project is under development, please do not depend on it yet as things may break. MinSQL MinSQL is a log search engine designed with simpli

A high level diffing library for rust based on diffs
A high level diffing library for rust based on diffs

Similar: A Diffing Library Similar is a dependency free crate for Rust that implements different diffing algorithms and high level interfaces for it.

An extremely high performance matching engine written in Rust.

Galois Introduction Galois is an extremely high performance matching engine written in Rust, typically used for the crypto currency exchange service.

High-performance link shortener

shorty High-performance link shortener written in Rust ๐Ÿ’พ Hosting In addition to being easy to build from source, shorty is available as a Docker imag

High-performance runtime for data analytics applications

Weld Documentation Weld is a language and runtime for improving the performance of data-intensive applications. It optimizes across libraries and func

Comments
  • I want arithmetic ops which return error instead of losing precision

    I want arithmetic ops which return error instead of losing precision

    I want lib for monetary calculations. decimal-rs sometimes lose precision, and I don't like that. For example, 1.0000000000000000000000000001 * 1.0000000000000000000000000001 is 1.0000000000000000000000000002 under decimal-rs (this is incorrect from mathematical point of view). (I used 0.1.36.) Even checked_mul gives same answer instead of returning error. I want some function (say, exact_mul and similar names for addition and subtraction), which returns Ok (or Some) when a result can be exactly represented and Err (or None) if not.

    In my program (assuming my program is bug-free) such precision losing should never happen. But as we all know we can never be sure that there is no bugs! So I want this exact_mul. I would replace my checked_mul with exact_mul (with unwrap). And if I ever lose precision, I will discover this, because I will see panic

    There is similar bug in rust-decimal https://github.com/paupino/rust-decimal/issues/515

    opened by safinaskar 2
  • Question about float -> decimal conversion

    Question about float -> decimal conversion

    Hi folks,

    I admittedly don't know a lot about floating point numbers so I just wanted to ask if some of the conversion results we are seeing are correct:

    • Decimal::try_from(0.1_f32).unwrap() == Decimal::from_str("0.10000000000000001").unwrap()
    • Decimal::try_from(0.2_f32).unwrap() == Decimal::from_str("0.20000000000000001").unwrap()
    • Decimal::try_from(0.3_f32).unwrap() == Decimal::from_str("0.29999999999999999").unwrap()
    • Decimal::try_from(0.4_f32).unwrap() == Decimal::from_str("0.40000000000000002").unwrap()

    Like I said I don't know much about floating point conversion so if these are the technically correct results we will just account for them in our code but I wanted to check with you first.

    Thank you!

    opened by lukewestby 1
Releases(v0.1.41)
Owner
CoD
Conquest of Data
CoD
Rust-tokenizer offers high-performance tokenizers for modern language models, including WordPiece, Byte-Pair Encoding (BPE) and Unigram (SentencePiece) models

rust-tokenizers Rust-tokenizer offers high-performance tokenizers for modern language models, including WordPiece, Byte-Pair Encoding (BPE) and Unigra

null 165 Jan 1, 2023
High-performance time series downsampling algorithms for visualization

tsdownsample ?? Time series downsampling algorithms for visualization Features โœจ Fast: written in rust with PyO3 bindings leverages optimized argminma

PreDiCT.IDLab 5 Dec 8, 2022
A Decimal Implementation written in pure Rust suitable for financial calculations.

Decimal โ€ƒ A Decimal implementation written in pure Rust suitable for financial calculations that require significant integral and fractional digits wi

Paul Mason 702 Jan 5, 2023
Get unix time (nanoseconds) in blazing low latency with high precision

RTSC Get unix time (nanoseconds) in blazing low latency with high precision. About 5xx faster than SystemTime::now(). Performance OS CPU benchmark rts

โ…ขx 8 Jul 14, 2022
Sub-pixel precision light spot rendering library for astronomy and video tracking applications.

Planetarium Sub-pixel precision light spot rendering library for astronomy and video tracking applications. Example usage use planetarium::{Canvas, Sp

Sergey Kvachonok 5 Mar 27, 2022
Extended precision integer Rust library. Provides signed/unsigned integer 256 to 2048.

Extended precision integer Rust library. Provides signed/unsigned integer 256 to 2048.

Mohanson 4 Jul 28, 2022
๐Ÿ’ฃ SMH โ€“ a computer vision project for automatic, precision mortar strike calculations in Squad

?? Squad Mortar Helper (SMH) SMH is a computer vision toy project aimed at automating mortar calculations in the game Squad Download demo.mp4 Requirem

William 18 Dec 26, 2022
Multiple precision floating point numbers implemented purely in Rust.

Multiple precision floating point numbers implemented purely in Rust. Rationale There are several notable implementations of numbers with increased pr

null 11 Nov 23, 2022
A high-performance, high-reliability observability data pipeline.

Quickstart โ€ข Docs โ€ข Guides โ€ข Integrations โ€ข Chat โ€ข Download What is Vector? Vector is a high-performance, end-to-end (agent & aggregator) observabilit

Timber 12.1k Jan 2, 2023
kytan: High Performance Peer-to-Peer VPN in Rust

kytan: High Performance Peer-to-Peer VPN kytan is a high performance peer to peer VPN written in Rust. The goal is to to minimize the hassle of config

Chang Lan 368 Dec 31, 2022