rs_abbreviation_number/
lib.rs

1mod calculation;
2
3pub mod time;
4use calculation::*;
5
6pub trait NumericAbbreviate {
7    ///  An Extension For Abbreviating Number.
8    /// # Example
9    /// ```
10    /// use rs_abbreviation_number::*;
11    /// let number=110_000;
12    /// let result=number.abbreviate_number(&Default::default());
13    /// assert_eq!("110K",result);
14    ///
15    /// let number=0.000_1;
16    /// let result=number.abbreviate_number(&AbbreviationOptions{separator:"-".to_string(),padding:0});
17    /// assert_eq!("100-μ",result);
18    /// ```
19    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String;
20}
21
22pub trait NumericUnAbbreviate {
23    ///  An Extension For UnAbbreviating Number.
24    /// # Example
25    /// ```
26    /// use rs_abbreviation_number::*;
27    /// let input="110K";
28    /// let result=input.unabbreviate_number();
29    /// assert_eq!(110_000.0,result);
30    ///
31    /// let input="1n";
32    /// let result=input.unabbreviate_number();
33    /// assert_eq!(0.000_000_001,result);
34    ///
35    /// ```
36
37    fn unabbreviate_number(&self) -> f64;
38}
39
40impl NumericAbbreviate for isize {
41    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
42        handle_abbreviation(*self as f64, options)
43    }
44}
45
46impl NumericAbbreviate for f64 {
47    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
48        handle_abbreviation(*self, options)
49    }
50}
51
52impl NumericAbbreviate for f32 {
53    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
54        handle_abbreviation(*self as f64, options)
55    }
56}
57
58impl NumericAbbreviate for i128 {
59    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
60        handle_abbreviation(*self as f64, options)
61    }
62}
63
64impl NumericAbbreviate for i64 {
65    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
66        handle_abbreviation(*self as f64, options)
67    }
68}
69
70impl NumericAbbreviate for i32 {
71    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
72        handle_abbreviation(*self as f64, options)
73    }
74}
75
76impl NumericAbbreviate for i16 {
77    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
78        handle_abbreviation(*self as f64, options)
79    }
80}
81
82impl NumericAbbreviate for i8 {
83    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
84        handle_abbreviation(*self as f64, options)
85    }
86}
87
88impl NumericAbbreviate for usize {
89    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
90        handle_abbreviation(*self as f64, options)
91    }
92}
93
94impl NumericAbbreviate for u128 {
95    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
96        handle_abbreviation(*self as f64, options)
97    }
98}
99
100impl NumericAbbreviate for u64 {
101    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
102        handle_abbreviation(*self as f64, options)
103    }
104}
105
106impl NumericAbbreviate for u32 {
107    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
108        handle_abbreviation(*self as f64, options)
109    }
110}
111
112impl NumericAbbreviate for u16 {
113    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
114        handle_abbreviation(*self as f64, options)
115    }
116}
117
118impl NumericAbbreviate for u8 {
119    fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
120        handle_abbreviation(*self as f64, options)
121    }
122}
123
124impl NumericUnAbbreviate for String {
125    fn unabbreviate_number(&self) -> f64 {
126        unabbreviate_number(self)
127    }
128}
129
130impl NumericUnAbbreviate for &str {
131    fn unabbreviate_number(&self) -> f64 {
132        unabbreviate_number(self)
133    }
134}
135
136fn handle_abbreviation(number: f64, options: &AbbreviationOptions) -> String {
137    if number.abs() >= 1.0 || number == 0.0 {
138        abbreviate_number(number, options)
139    } else {
140        abbreviate_fraction_number(number, options)
141    }
142}
143
144pub struct AbbreviationOptions {
145    pub separator: String,
146    pub padding: usize,
147}
148
149impl Default for AbbreviationOptions {
150    fn default() -> Self {
151        Self {
152            separator: "".to_string(),
153            padding: 0,
154        }
155    }
156}