summaryrefslogtreecommitdiffstats
path: root/intl/icu/source/i18n/digitgrouping.h
blob: 934fd4b9aba5fe6d1f378b8ad1b6fbbb8a7880f8 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2015, International Business Machines
* Corporation and others.  All Rights Reserved.
*******************************************************************************
* digitgrouping.h
*
* created on: 2015jan6
* created by: Travis Keep
*/

#ifndef __DIGITGROUPING_H__
#define __DIGITGROUPING_H__

#include "unicode/uobject.h"
#include "unicode/utypes.h"

U_NAMESPACE_BEGIN

class IntDigitCountRange;

/**
 * The digit grouping policy.
 */
class U_I18N_API DigitGrouping : public UMemory {
public:
    /**
     * Default is no digit grouping.
     */
    DigitGrouping() : fGrouping(0), fGrouping2(0), fMinGrouping(0) { }

    /**
     * Returns TRUE if this object is equal to rhs.
     */
    UBool equals(const DigitGrouping &rhs) const {
        return ((fGrouping == rhs.fGrouping) && 
                (fGrouping2 == rhs.fGrouping2) &&
                (fMinGrouping == rhs.fMinGrouping));
    }

    /**
     * Returns true if a separator is needed after a particular digit.
     * @param digitsLeftOfDecimal the total count of digits left of the
     *  decimal.
     * @param digitPos 0 is the one's place; 1 is the 10's place; -1 is the
     *   1/10's place etc.
     */
    UBool isSeparatorAt(int32_t digitsLeftOfDecimal, int32_t digitPos) const;

    /**
     * Returns the total number of separators to be used to format a particular
     * number.
     * @param digitsLeftOfDecimal the total number of digits to the left of
     *   the decimal.
     */
    int32_t getSeparatorCount(int32_t digitsLeftOfDecimal) const;

    /**
     * Returns true if grouping is used FALSE otherwise. When
     * isGroupingUsed() returns FALSE; isSeparatorAt always returns FALSE
     * and getSeparatorCount always returns 0.
     */
    UBool isGroupingUsed() const { return fGrouping > 0; }

    /**
     * Returns TRUE if this instance would not add grouping separators
     * when formatting value using the given constraint on digit count.
     *
     * @param value the value to format.
     * @param range the minimum and maximum digits for formatting value.
     */
    UBool isNoGrouping(
            int32_t positiveValue, const IntDigitCountRange &range) const;

    /**
     * Clears this instance so that digit grouping is not in effect.
     */
    void clear();

public:

    /**
     * Primary grouping size. A value of 0, the default, or a negative
     * number causes isGroupingUsed() to return FALSE.
     */
    int32_t fGrouping;

    /**
     * Secondary grouping size. If > 0, this size is used instead of
     * 'fGrouping' for all but the group just to the left of the decimal
     * point. The default value of 0, or a negative value indicates that
     * there is no secondary grouping size.
     */
    int32_t fGrouping2;

    /**
     * If set (that is > 0), uses no grouping separators if fewer than
     * (fGrouping + fMinGrouping) digits appear left of the decimal place.
     * The default value for this field is 0.
     */
    int32_t fMinGrouping;
private:
    UBool isGroupingEnabled(int32_t digitsLeftOfDecimal) const;
    int32_t getGrouping2() const;
    int32_t getMinGrouping() const;
};

U_NAMESPACE_END

#endif  // __DIGITGROUPING_H__