Imported Upstream version 58.1
[platform/upstream/icu.git] / source / i18n / digitgrouping.h
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 * digitgrouping.h
9 *
10 * created on: 2015jan6
11 * created by: Travis Keep
12 */
13
14 #ifndef __DIGITGROUPING_H__
15 #define __DIGITGROUPING_H__
16
17 #include "unicode/uobject.h"
18 #include "unicode/utypes.h"
19
20 U_NAMESPACE_BEGIN
21
22 class IntDigitCountRange;
23
24 /**
25  * The digit grouping policy.
26  */
27 class U_I18N_API DigitGrouping : public UMemory {
28 public:
29     /**
30      * Default is no digit grouping.
31      */
32     DigitGrouping() : fGrouping(0), fGrouping2(0), fMinGrouping(0) { }
33
34     /**
35      * Returns TRUE if this object is equal to rhs.
36      */
37     UBool equals(const DigitGrouping &rhs) const {
38         return ((fGrouping == rhs.fGrouping) && 
39                 (fGrouping2 == rhs.fGrouping2) &&
40                 (fMinGrouping == rhs.fMinGrouping));
41     }
42
43     /**
44      * Returns true if a separator is needed after a particular digit.
45      * @param digitsLeftOfDecimal the total count of digits left of the
46      *  decimal.
47      * @param digitPos 0 is the one's place; 1 is the 10's place; -1 is the
48      *   1/10's place etc.
49      */
50     UBool isSeparatorAt(int32_t digitsLeftOfDecimal, int32_t digitPos) const;
51
52     /**
53      * Returns the total number of separators to be used to format a particular
54      * number.
55      * @param digitsLeftOfDecimal the total number of digits to the left of
56      *   the decimal.
57      */
58     int32_t getSeparatorCount(int32_t digitsLeftOfDecimal) const;
59
60     /**
61      * Returns true if grouping is used FALSE otherwise. When
62      * isGroupingUsed() returns FALSE; isSeparatorAt always returns FALSE
63      * and getSeparatorCount always returns 0.
64      */
65     UBool isGroupingUsed() const { return fGrouping > 0; }
66
67     /**
68      * Returns TRUE if this instance would not add grouping separators
69      * when formatting value using the given constraint on digit count.
70      *
71      * @param value the value to format.
72      * @param range the minimum and maximum digits for formatting value.
73      */
74     UBool isNoGrouping(
75             int32_t positiveValue, const IntDigitCountRange &range) const;
76
77     /**
78      * Clears this instance so that digit grouping is not in effect.
79      */
80     void clear();
81
82 public:
83
84     /**
85      * Primary grouping size. A value of 0, the default, or a negative
86      * number causes isGroupingUsed() to return FALSE.
87      */
88     int32_t fGrouping;
89
90     /**
91      * Secondary grouping size. If > 0, this size is used instead of
92      * 'fGrouping' for all but the group just to the left of the decimal
93      * point. The default value of 0, or a negative value indicates that
94      * there is no secondary grouping size.
95      */
96     int32_t fGrouping2;
97
98     /**
99      * If set (that is > 0), uses no grouping separators if fewer than
100      * (fGrouping + fMinGrouping) digits appear left of the decimal place.
101      * The default value for this field is 0.
102      */
103     int32_t fMinGrouping;
104 private:
105     UBool isGroupingEnabled(int32_t digitsLeftOfDecimal) const;
106     int32_t getGrouping2() const;
107     int32_t getMinGrouping() const;
108 };
109
110 U_NAMESPACE_END
111
112 #endif  // __DIGITGROUPING_H__