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