1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use super::{SeparatorPolicy, policies};

/// Provides methods for formatting numbers with separators between the digits.
pub trait Separable {
    /// Inserts a comma every three digits from the right.
    ///
    /// This is equivalent to `self.separate_by_policy(policies::COMMA_SEPARATOR)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use thousands::*;
    /// assert_eq!( 12345.separate_with_commas(), "12,345" );
    /// ```
    fn separate_with_commas(&self) -> String {
        self.separate_by_policy(policies::COMMA_SEPARATOR)
    }

    /// Inserts a space every three digits from the right.
    ///
    /// This is equivalent to `self.separate_by_policy(policies::SPACE_SEPARATOR)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use thousands::*;
    /// assert_eq!( 12345.separate_with_spaces(), "12 345" );
    /// ```
    fn separate_with_spaces(&self) -> String {
        self.separate_by_policy(policies::SPACE_SEPARATOR)
    }

    /// Inserts a period every three digits from the right.
    ///
    /// This is equivalent to `self.separate_by_policy(policies::DOT_SEPARATOR)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use thousands::*;
    /// assert_eq!( 12345.separate_with_dots(), "12.345" );
    /// ```
    fn separate_with_dots(&self) -> String {
        self.separate_by_policy(policies::DOT_SEPARATOR)
    }

    /// Inserts an underscore every three digits from the right.
    ///
    /// This is equivalent to `self.separate_by_policy(policies::UNDERSCORE_SEPARATOR)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use thousands::*;
    /// assert_eq!( 12345.separate_with_underscores(), "12_345" );
    /// ```
    fn separate_with_underscores(&self) -> String {
        self.separate_by_policy(policies::UNDERSCORE_SEPARATOR)
    }

    /// Adds separators according to the given [`SeparatorPolicy`].
    ///
    /// # Examples
    ///
    /// ```
    /// use thousands::{Separable, SeparatorPolicy, digits};
    ///
    /// let policy = SeparatorPolicy {
    ///     separator:  ":",
    ///     groups:     &[1, 2, 3, 4],
    ///     digits:     digits::ASCII_DECIMAL,
    /// };
    ///
    /// assert_eq!( 1234567654321u64.separate_by_policy(policy),
    ///             "123:4567:654:32:1" );
    /// ```
    ///
    /// [`SeparatorPolicy`]: struct.SeparatorPolicy.html
    fn separate_by_policy(&self, policy: SeparatorPolicy) -> String;
}