| Crates.io | cowry |
| lib.rs | cowry |
| version | 0.1.0 |
| created_at | 2025-04-19 21:43:03.740554+00 |
| updated_at | 2025-04-19 21:43:03.740554+00 |
| description | A micro-library for precise currency math and rounding control in Rust. |
| homepage | https://github.com/matt-kay/cowry_rs.git |
| repository | https://github.com/matt-kay/cowry_rs.git |
| max_upload_size | |
| id | 1641168 |
| size | 31,455 |
Cowry is a robust and flexible money type written in Rust, designed to represent and manipulate monetary values accurately using minor units (like cents, kobo, or satoshi). It supports multiple currencies, rounding modes, arithmetic operations, formatting, and serialization.
Owo is a Rust struct for handling money in minor units (like cents, kobo, etc.). It supports safe arithmetic, precision rounding, formatting, serialization, and batch operations.
150)serdeOwo instanceuse cowry::prelude::*;
let ngn = Currency::new("NGN", "โฆ", 2);
let owo_ngn = Owo::new(500,ngn);
let usd = Currency::new("USD", "$", 2);
let owo_usd = Owo::new(100,usd);
let jpy = Currency::new("JPY", "ยฅ", 0);
let owo_jpy = Owo::new(200,jpy);
let btc = Currency::new("BTC", "โฟ", 8);
let owo_btc = Owo::new(200,btc);
assert_eq!(owo_ngn.format(),"โฆ5.00");
assert_eq!(owo_usd.format(),"$1.00");
assert_eq!(owo_jpy.format(),"ยฅ200");
assert_eq!(owo_btc.format(),"โฟ0.00000200");
use cowry::prelude::*;
let ngn = Currency::new("NGN", "โฆ", 2);
let owo = Owo::new(500,ngn);
assert_eq!(owo.format(),"โฆ5.00");
use cowry::prelude::*;
let json_str = r#"{"amount": 500,"currency": { "code": "EUR","symbol": "โฌ","precision": 2 }}"#;
let owo = Owo::from_json(json_str).unwrap();
assert_eq!(owo.amount, 500);
assert_eq!(owo.currency.code, "EUR");
assert_eq!(owo.currency.precision, 2);
use cowry::prelude::*;
let ngn = Currency::new("NGN", "โฆ", 2);
let owo = Owo::new(500,ngn);
let json = owo.to_json().unwrap();
assert_eq!(json, r#"{"amount":500,"currency":{"code":"NGN","symbol":"โฆ","precision":2}}"#);
use cowry::prelude::*;
let usd = Currency::new("USD", "$", 2);
let price_1 = Owo::new(1000,usd.clone()); //$10.00
let price_2 = Owo::new(2000,usd.clone()); //$20.00
let result = price_1 + price_2; //$30.00
let value = Owo::new(105, ngn); // โฆ1.05
let rounded = value.multiply_with_mode(2.5, RoundingMode::Nearest); // โฆ2.63
pub struct Owo {
pub amount: i64,
pub currency: Currency,
}
amount: The value in minor unitscurrency: The currency type (e.g., NGN, USD), includes precision settingsnew(amount: i64, currency: Currency) -> Owo
Create a new Owo instance.
from_json(json_str: &str) -> Result<Owo, serde_json::Error>
Deserialize a JSON string to Owo.
to_json(&self) -> Result<String, OwoError>
Serialize the Owo instance to JSON.
get_amount() -> i64
Returns the internal amount in minor units.
get_currency() -> &str
Returns the currency code as a string.
get_precision() -> u8
Gets the number of decimal places based on the currency.
format() -> String
Returns a human-readable string like "โฆ1,200.00".
round_to_precision(&mut self)
Rounds the internal amount to the currencyโs default precision.
round_amount(raw: f64) -> i64
Rounds a float value based on currency precision (internal use).
round_amount_with_mode(raw: f64, mode: RoundingMode) -> i64
Rounds with a specific mode like RoundHalfUp, RoundDown, etc.
multiply(scalar: f64) -> Owodivide(scalar: f64) -> Owopercentage(percent: f64) -> Owomultiply_with_mode(scalar: f64, mode: RoundingMode) -> Owodivide_with_mode(scalar: f64, mode: RoundingMode) -> Owopercentage_with_mode(percent: f64, mode: RoundingMode) -> Owoeq(rhs: &Self) -> boollt(rhs: &Self) -> boolgt(rhs: &Self) -> boolImplements PartialEq and PartialOrd traits as well.
You can use arithmetic operators directly with Owo:
+, -, *, /, unary -
Vec<Owo>)The BatchOperations trait is implemented for Vec<Owo>:
multiply_all(scalar: f64) -> Vec<Owo>divide_all(scalar: f64) -> Vec<Owo>percentage_all(percent: f64) -> Vec<Owo>All have _with_mode(...) versions to support rounding strategy.
let price = Owo::new(5000, Currency::NGN); // โฆ50.00
let discounted = price.percentage(10.0); // โฆ5.00
println!("{}", discounted.format()); // "โฆ5.00"
You can run tests to validate functionality:
cargo test
serde for JSON serializationCurrency, RoundingMode, and OwoError implementationsYou can use Owo as part of your domain-driven design, financial models, or event-sourced accounting systems.
MIT or Apache-2.0