1 /* 2 ******************************************************************************* * Copyright (C) 2015, International Business Machines 3 * Corporation and others. All Rights Reserved. 4 ******************************************************************************* 5 * visibledigits.h 6 * 7 * created on: 2015jun20 8 * created by: Travis Keep 9 */ 10 11 #ifndef __VISIBLEDIGITS_H__ 12 #define __VISIBLEDIGITS_H__ 13 14 #include "unicode/utypes.h" 15 16 #if !UCONFIG_NO_FORMATTING 17 18 #include "unicode/uobject.h" 19 20 #include "charstr.h" 21 #include "digitinterval.h" 22 23 U_NAMESPACE_BEGIN 24 25 class DigitList; 26 27 /** 28 * VisibleDigits represents the digits visible for formatting. 29 * Once initialized using a FixedPrecision instance, VisibleDigits instances 30 * remain unchanged until they are initialized again. A VisibleDigits with 31 * a numeric value equal to 3.0 could be "3", "3.0", "3.00" or even "003.0" 32 * depending on settings of the FixedPrecision instance used to initialize it. 33 */ 34 class U_I18N_API VisibleDigits : public UMemory { 35 public: 36 VisibleDigits() : fExponent(0), fFlags(0), fAbsIntValue(0), fAbsIntValueSet(FALSE), fAbsDoubleValue(0.0), fAbsDoubleValueSet(FALSE) { } 37 38 UBool isNegative() const; 39 UBool isNaN() const; 40 UBool isInfinite() const; 41 UBool isNaNOrInfinity() const; 42 43 /** 44 * Gets the digit at particular exponent, if number is 987.6, then 45 * getDigit(2) == 9 and gitDigit(0) == 7 and gitDigit(-1) == 6. 46 * If isNaN() or isInfinity() return TRUE, then the result of this 47 * function is undefined. 48 */ 49 int32_t getDigitByExponent(int32_t digitPos) const; 50 51 /** 52 * Returns the digit interval which indicates the leftmost and rightmost 53 * position of this instance. 54 * If isNaN() or isInfinity() return TRUE, then the result of this 55 * function is undefined. 56 */ 57 const DigitInterval &getInterval() const { return fInterval; } 58 59 /** 60 * Gets the parameters needed to create a FixedDecimal. 61 */ 62 void getFixedDecimal(double &source, int64_t &intValue, int64_t &f, int64_t &t, int32_t &v, UBool &hasIntValue) const; 63 64 65 private: 66 /** 67 * The digits, least significant first. Both the least and most 68 * significant digit in this list are non-zero; however, digits in the 69 * middle may be zero. This field contains values between (char) 0, and 70 * (char) 9 inclusive. 71 */ 72 CharString fDigits; 73 74 /** 75 * The range of displayable digits. This field is needed to account for 76 * any leading and trailing zeros which are not stored in fDigits. 77 */ 78 DigitInterval fInterval; 79 80 /** 81 * The exponent value of the least significant digit in fDigits. For 82 * example, fExponent = 2 and fDigits = {7, 8, 5} represents 58700. 83 */ 84 int32_t fExponent; 85 86 /** 87 * Contains flags such as NaN, Inf, and negative. 88 */ 89 int32_t fFlags; 90 91 /** 92 * Contains the absolute value of the digits left of the decimal place 93 * if fAbsIntValueSet is TRUE 94 */ 95 int64_t fAbsIntValue; 96 97 /** 98 * Indicates whether or not fAbsIntValue is set. 99 */ 100 UBool fAbsIntValueSet; 101 102 /** 103 * Contains the absolute value of the value this instance represents 104 * if fAbsDoubleValueSet is TRUE 105 */ 106 double fAbsDoubleValue; 107 108 /** 109 * Indicates whether or not fAbsDoubleValue is set. 110 */ 111 UBool fAbsDoubleValueSet; 112 113 void setNegative(); 114 void setNaN(); 115 void setInfinite(); 116 void clear(); 117 double computeAbsDoubleValue() const; 118 UBool isOverMaxDigits() const; 119 120 VisibleDigits(const VisibleDigits &); 121 VisibleDigits &operator=(const VisibleDigits &); 122 123 friend class FixedPrecision; 124 friend class VisibleDigitsWithExponent; 125 }; 126 127 /** 128 * A VisibleDigits with a possible exponent. 129 */ 130 class U_I18N_API VisibleDigitsWithExponent : public UMemory { 131 public: 132 VisibleDigitsWithExponent() : fHasExponent(FALSE) { } 133 const VisibleDigits &getMantissa() const { return fMantissa; } 134 const VisibleDigits *getExponent() const { 135 return fHasExponent ? &fExponent : NULL; 136 } 137 void clear() { 138 fMantissa.clear(); 139 fExponent.clear(); 140 fHasExponent = FALSE; 141 } 142 UBool isNegative() const { return fMantissa.isNegative(); } 143 UBool isNaN() const { return fMantissa.isNaN(); } 144 UBool isInfinite() const { return fMantissa.isInfinite(); } 145 private: 146 VisibleDigitsWithExponent(const VisibleDigitsWithExponent &); 147 VisibleDigitsWithExponent &operator=( 148 const VisibleDigitsWithExponent &); 149 VisibleDigits fMantissa; 150 VisibleDigits fExponent; 151 UBool fHasExponent; 152 153 friend class ScientificPrecision; 154 friend class FixedPrecision; 155 }; 156 157 158 U_NAMESPACE_END 159 #endif /* #if !UCONFIG_NO_FORMATTING */ 160 #endif // __VISIBLEDIGITS_H__ 161