This crate provides a set of operations for working with financial data, more specifically, avoiding the usage of floating point types.
use abklabs_financial_operations::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.
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.
use abklabs_financial_operations::CheckedDecimalOperations;add_decimals_checkedsub_decimals_checkedmul_decimals_checkeddiv_decimals_checkedrem_decimals_checked
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.
use abklabs_financial_operations::DecimalOperations;add_decimalssub_decimalsmul_decimalsdiv_decimalsrem_decimals