https://github.com/0xC0A1/financial-ops
This crate provides a set of operations for working with financial data, more specifically, avoiding the usage of floating point types.
https://github.com/0xC0A1/financial-ops
blockchain finance integer-arithmetic rust solana
Last synced: 11 months ago
JSON representation
This crate provides a set of operations for working with financial data, more specifically, avoiding the usage of floating point types.
- Host: GitHub
- URL: https://github.com/0xC0A1/financial-ops
- Owner: 0xC0A1
- License: mit
- Created: 2024-07-31T18:38:05.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-21T17:19:40.000Z (almost 2 years ago)
- Last Synced: 2025-07-02T04:45:56.763Z (11 months ago)
- Topics: blockchain, finance, integer-arithmetic, rust, solana
- Language: Rust
- Homepage: https://docs.rs/financial-ops
- Size: 10.7 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Financial Ops
[](https://crates.io/crates/financial-ops)
[](https://docs.rs/financial-ops)
[]()
This crate provides a set of operations for working with financial data, more specifically, avoiding
the usage of floating point types.
## Usage
```rust
use financial_ops::CheckedDecimalOperations;
fn test_add_decimals() {
let a: u64 = 1_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.add_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 3_0000);
assert_eq!(decimals, 4);
let a: u32 = 123_45;
let a_decimals = 2;
let b: u32 = 0_45;
let b_decimals = 2;
let (result, decimals) = a.add_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 123_90);
assert_eq!(decimals, 2);
}
```
Very useful when dealing with money or blockchain transactions.
## Supported operations
### Checked
This set of operations will return an `Result` with the result and the number of decimals,
if the operation is successful. If the operation is not successful, it will return a `DecimalOperationError`.
```rust
use financial_ops::CheckedDecimalOperations;
```
- `add_decimals_checked`
- `sub_decimals_checked`
- `mul_decimals_checked`
- `div_decimals_checked`
- `rem_decimals_checked`
### Unchecked
This set of operations will return the result and the number of decimals, without any checks,
carrying the underlying operation way of handling overflows and underflows.
```rust
use financial_ops::DecimalOperations;
```
- `add_decimals`
- `sub_decimals`
- `mul_decimals`
- `div_decimals`
- `rem_decimals`