#money #comparison #mutation #format #conversion #values #lets

dinero

Dinero 允许您表达货币值。您可以执行更改、转换、比较,对其进行广泛格式化,并总体上使应用程序中的货币操作更容易、更安全。

5 个版本

0.0.11 2022年11月28日
0.0.10 2022年10月23日

57金融

42 每月下载次数
用于 dinero-exchange-cli

MIT 许可证

99KB
3K SLoC

Dinero-rust

Stability: alpha Crates.io Crates.io Crates.io

Dinero 是 Dinero.js 的 Rust 版本
Dinero 允许您在 Rust 中创建、计算和格式化货币。
docs.rs/dinero


📦 安装

$ cargo add dinero

⚡️ 快速开始

Dinero 对象是最小的。API 受 dinero.js 极大启发,除非有更适合 Rust 的实现方式。

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); // Similar API as Dinero.js

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

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

🦀 免责声明

我使用此项目来学习 Rust。我正在通过语言和生态系统进行学习。

请注意,Dinero 的当前版本 不稳定。肯定会进行重大更改。

📜 许可证

MIT

无运行时依赖