compa_decimal
A compact and efficient decimal system using a custom character set for representing large numbers in fewer characters.
Features
- Custom Character Set: Uses numbers, uppercase and lowercase letters, and special characters for a base-96 numeral system.
- Supports Multiple Numeric Types: Easily convert between unsigned integers (
u8,u16,u32,u64,u128) and theCompaDecimalformat. - Now Supports arbitrarily large numbers: Easily convert between
BigUintand theCompaDecimalformat. - Conversion Utilities: Convert between standard decimal numbers and the compact
CompaDecimalrepresentation.
Usage
Add to your Cargo.toml:
[]
= "0.1.1"
Import in your Rust code:
use CompaDecimal;
Examples
Creating a New CompaDecimal
let compa = new;
assert_eq!;
Parsing from &str
let compa: CompaDecimal = "123asd".parse.unwrap;
assert_eq!;
Decimal to CompaDecimal
let compa = .unwrap;
assert_eq!;
BigUint to CompaDecimal
let compa = .unwrap;
assert_eq!;
Get Value
let compa = "1LY7VK"..unwrap;
let compa_value = compa.get_value;
assert_eq!;
Increment and Decrement
let compa = "A1"..unwrap;
let increased = compa.plus_one.unwrap;
assert_eq!;
let decreased = increased.minus_one.unwrap;
assert_eq!;
Add and Subtract
let compa = "ASr35"..unwrap;
let new_compa = compa.add.unwrap;
assert_eq!;
let subtracted = new_compa.subtract.unwrap;
assert_eq!;
Compare
use Ordering;
let compa = "axswF9"..unwrap;
assert_eq!;
Length
let compa = "1LY7VK"..unwrap;
assert_eq!;
Convert Back to Decimal
let compa = "1LY7VK"..unwrap;
let decimal: u64 = compa.to_decimal.unwrap;
assert_eq!;
Convert Back to BigUint
let compa = "1T~PC"..unwrap;
let biguint = compa.to_biguint.unwrap;
assert_eq!;
Compare to string
You can compare a CompaDecimal directly with a string:
let compa = "123asd"..unwrap;
assert_eq!;
assert_ne!;
Attempt to convert string to CompaDecimal
You can use TryFrom to create a CompaDecimal from a string, which returns a Result:
use TryFrom;
let compa = try_from.unwrap;
assert_eq!;
// This will return an error if the string contains invalid characters
let invalid = try_from;
assert!;
You can also use try_into for ergonomic conversion:
use TryInto;
let compa: CompaDecimal = "123asd".try_into.unwrap;
assert_eq!;
Error Handling
Most methods return a Result and will return an error if:
- The input contains invalid characters.
- The operation would result in a negative value.
- The value is too large for the target integer type.
License
Licensed under MIT OR Apache-2.0.