Crate dinero

source ·
Expand description

Dinero

Dinero lets you express monetary values in Rust. The library is a port of dinerojs v2 and uses similar paradigms.

Money is complex, and the primitives of the language aren’t enough to properly represent it. Dinero is a Rust library that lets you express monetary values, but also perform mutations, conversions, comparisons, formatting, and overall make money manipulation easier and safer in your application.

You can perform mutations, conversions, comparisons, format them extensively, and overall make money manipulation in your application easier and safer.

Getting started

You can create Dinero objects with the associated function Dinero::new()

use dinero::{api::add, currencies::USD, format::to_unit, Dinero};

// Create a Dinero object of value 8.5 USD (the default scale for USD is 2)
let d1 = Dinero::new(850, USD, None);
// Create a Dinero object of value 5 USD with a custom scale 3
let d2 = Dinero::new(5000, USD, Some(3));

// Add the 2 Dineros, the value is stored in the result Dinero without modifying d1 and d2
let result = add(&d1, &d2);

// Or you can use the standard operators
// let result = d1 + d2;

match result {
  Ok(value) => println!("{} USD", to_unit(value, None, None)), // 13.5 USD
  Err(_) => println!("Error adding d1+d2"),
}

Modules

Structs