icu_decimal/
options.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5//! Options for [`DecimalFormatter`](crate::DecimalFormatter).
6
7/// A bag of options defining how numbers will be formatted by
8/// [`DecimalFormatter`](crate::DecimalFormatter).
9#[derive(Debug, Eq, PartialEq, Clone, Copy, Default, Hash)]
10#[non_exhaustive]
11pub struct DecimalFormatterOptions {
12    /// When to render grouping separators.
13    ///
14    /// Default is [`GroupingStrategy::Auto`]
15    pub grouping_strategy: Option<GroupingStrategy>,
16}
17
18impl From<GroupingStrategy> for DecimalFormatterOptions {
19    fn from(grouping_strategy: GroupingStrategy) -> Self {
20        Self {
21            grouping_strategy: Some(grouping_strategy),
22        }
23    }
24}
25
26/// Configuration for how often to render grouping separators.
27///
28/// # Examples
29///
30/// ```
31/// use icu::decimal::options;
32/// use icu::decimal::DecimalFormatter;
33/// use icu::locale::Locale;
34/// use writeable::assert_writeable_eq;
35///
36/// let locale = Default::default();
37/// let mut options: options::DecimalFormatterOptions = Default::default();
38/// options.grouping_strategy = Some(options::GroupingStrategy::Min2);
39/// let formatter = DecimalFormatter::try_new(locale, options)
40///     .expect("locale should be present");
41///
42/// let one_thousand = 1000.into();
43/// assert_writeable_eq!(formatter.format(&one_thousand), "1000");
44///
45/// let ten_thousand = 10000.into();
46/// assert_writeable_eq!(formatter.format(&ten_thousand), "10,000");
47/// ```
48#[non_exhaustive]
49#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash, Default)]
50pub enum GroupingStrategy {
51    /// Render grouping separators according to locale preferences.
52    #[default]
53    Auto,
54
55    /// Never render grouping separators.
56    Never,
57
58    /// Always render grouping separators.
59    ///
60    /// For [`DecimalFormatter`](crate::DecimalFormatter), [`GroupingStrategy::Always`]
61    /// has the same behavior as [`GroupingStrategy::Auto`].
62    Always,
63
64    /// Render grouping separators only if there are at least 2 digits before the final grouping
65    /// separator. In most locales, this means that numbers between 1000 and 9999 do not get
66    /// grouping separators, but numbers 10,000 and above will.
67    Min2,
68}