1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * created on: 2015jan6
11 * created by: Travis Keep
14 #ifndef __DIGITINTERVAL_H__
15 #define __DIGITINTERVAL_H__
17 #include "unicode/uobject.h"
18 #include "unicode/utypes.h"
23 * An interval of digits.
24 * DigitIntervals are for fixed point formatting. A DigitInterval specifies
25 * zero or more integer digits and zero or more fractional digits. This class
26 * specifies particular digits in a number by their power of 10. For example,
27 * the digit position just to the left of the decimal is 0, and the digit
28 * position just left of that is 1. The digit position just to the right of
29 * the decimal is -1. The digit position just to the right of that is -2.
31 class U_I18N_API DigitInterval : public UMemory {
35 * Spans all integer and fraction digits
38 : fLargestExclusive(INT32_MAX), fSmallestInclusive(INT32_MIN) { }
42 * Makes this instance span all digits.
45 fLargestExclusive = INT32_MAX;
46 fSmallestInclusive = INT32_MIN;
50 * Returns TRUE if this interval contains this digit position.
52 UBool contains(int32_t digitPosition) const;
55 * Returns true if this object is the same as rhs.
57 UBool equals(const DigitInterval &rhs) const {
58 return ((fLargestExclusive == rhs.fLargestExclusive) &&
59 (fSmallestInclusive == rhs.fSmallestInclusive));
63 * Expand this interval so that it contains all of rhs.
65 void expandToContain(const DigitInterval &rhs);
68 * Shrink this interval so that it contains no more than rhs.
70 void shrinkToFitWithin(const DigitInterval &rhs);
73 * Expand this interval as necessary to contain digit with given exponent
74 * After this method returns, this interval is guaranteed to contain
77 void expandToContainDigit(int32_t digitExponent);
80 * Changes the number of digits to the left of the decimal point that
81 * this interval spans. If count is negative, it means span all digits
82 * to the left of the decimal point.
84 void setIntDigitCount(int32_t count);
87 * Changes the number of digits to the right of the decimal point that
88 * this interval spans. If count is negative, it means span all digits
89 * to the right of the decimal point.
91 void setFracDigitCount(int32_t count);
94 * Sets the least significant inclusive value to smallest. If smallest >= 0
95 * then least significant inclusive value becomes 0.
97 void setLeastSignificantInclusive(int32_t smallest) {
98 fSmallestInclusive = smallest < 0 ? smallest : 0;
102 * Sets the most significant exclusive value to largest.
103 * If largest <= 0 then most significant exclusive value becomes 0.
105 void setMostSignificantExclusive(int32_t largest) {
106 fLargestExclusive = largest > 0 ? largest : 0;
110 * If returns 8, the most significant digit in interval is the 10^7 digit.
111 * Returns INT32_MAX if this interval spans all digits to left of
114 int32_t getMostSignificantExclusive() const {
115 return fLargestExclusive;
119 * Returns number of digits to the left of the decimal that this
120 * interval includes. This is a synonym for getMostSignificantExclusive().
122 int32_t getIntDigitCount() const {
123 return fLargestExclusive;
127 * Returns number of digits to the right of the decimal that this
130 int32_t getFracDigitCount() const {
131 return fSmallestInclusive == INT32_MIN ? INT32_MAX : -fSmallestInclusive;
135 * Returns the total number of digits that this interval spans.
136 * Caution: If this interval spans all digits to the left or right of
137 * decimal point instead of some fixed number, then what length()
138 * returns is undefined.
140 int32_t length() const {
141 return fLargestExclusive - fSmallestInclusive;
145 * If returns -3, the least significant digit in interval is the 10^-3
146 * digit. Returns INT32_MIN if this interval spans all digits to right of
149 int32_t getLeastSignificantInclusive() const {
150 return fSmallestInclusive;
153 int32_t fLargestExclusive;
154 int32_t fSmallestInclusive;
159 #endif // __DIGITINTERVAL_H__