#currency #separator #localization #symbol #traits #interface #logic

null-kane

currency crate with the option to add your own currency localization logic

16 releases (8 stable)

new 2.0.4 Apr 25, 2024
2.0.3 Feb 26, 2024
2.0.2 Jan 13, 2024
1.0.2 Jan 5, 2024
0.1.4 Dec 4, 2023

#290 in Rust patterns

Download history 11/week @ 2024-01-05 4/week @ 2024-01-12 19/week @ 2024-02-02 18/week @ 2024-02-09 152/week @ 2024-02-16 140/week @ 2024-02-23 44/week @ 2024-03-01 44/week @ 2024-03-08 44/week @ 2024-03-15 3/week @ 2024-03-22 33/week @ 2024-03-29 23/week @ 2024-04-05 7/week @ 2024-04-12 11/week @ 2024-04-19

74 downloads per month

Custom license

35KB
906 lines

null-kane

This is a Rust crate designed for currency manipulation. It provides an interface via a trait for implementing localization methods. The trait enables retrieval of the currency separator, thousand separator, and currency symbol.

Features

Localization Methods

  • get_separator: Retrieves the separator used for the currency.
  • get_thousand_separator: Retrieves the thousand separator used for the currency.
  • get_currency_symbol: Retrieves the currency symbol.

Arithmetic Operations

The crate implements arithmetic operations for the following types:

  • Addition, multiplication, and division among currencies.
  • Addition, multiplication, and division between currencies and usize.
  • Addition, multiplication, and division between currencies and f32.
  • Addition, multiplication, and division between currencies and f64.

Constructors

From implementations for creating instances from f32 and f64.

Example

use null_kane::{Currency, CurrencyLocalization};

#[derive(Clone, Default, PartialEq, Eq)]
enum MyLocalization {
    #[default]
    Euro
}

impl CurrencyLocalization for MyLocalization {
    fn get_separator(&self) -> char {
        // Implementation of the separator for Euro
        ','
    }

    fn get_thousand_separator(&self) -> char {
        // Implementation of the thousand separator for Euro
        '.'
    }

    fn get_currency_symbol(&self) -> &'static str {
        // Implementation of the currency symbol for Euro
        ""
    }
}

fn main() {
    let euro_currency = MyLocalization::default;
    let amount_one = Currency::from(50.25_f32).with_locale(euro_currency.clone());
    let amount_two = Currency::from(30.75_f64).with_locale(euro_currency);

    let sum = amount_one + amount_two;
    let product = amount_one * 2.0;
    let division = amount_two / 3.0;

    println!("Sum: {}", sum);
    println!("Product: {}", product);
    println!("Division: {}", division);
}

TODO

  • implement a good solution for failing operations, for example if currency localizations don't match
  • consider using num trait

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~185KB