1*0e209d39SAndroid Build Coastguard Worker // © 2017 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker 4*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 5*0e209d39SAndroid Build Coastguard Worker 6*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING 7*0e209d39SAndroid Build Coastguard Worker #ifndef __NUMBER_DECIMALQUANTITY_H__ 8*0e209d39SAndroid Build Coastguard Worker #define __NUMBER_DECIMALQUANTITY_H__ 9*0e209d39SAndroid Build Coastguard Worker 10*0e209d39SAndroid Build Coastguard Worker #include <cstdint> 11*0e209d39SAndroid Build Coastguard Worker #include "unicode/umachine.h" 12*0e209d39SAndroid Build Coastguard Worker #include "standardplural.h" 13*0e209d39SAndroid Build Coastguard Worker #include "plurrule_impl.h" 14*0e209d39SAndroid Build Coastguard Worker #include "number_types.h" 15*0e209d39SAndroid Build Coastguard Worker 16*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 17*0e209d39SAndroid Build Coastguard Worker namespace number::impl { 18*0e209d39SAndroid Build Coastguard Worker 19*0e209d39SAndroid Build Coastguard Worker // Forward-declare (maybe don't want number_utils.h included here): 20*0e209d39SAndroid Build Coastguard Worker class DecNum; 21*0e209d39SAndroid Build Coastguard Worker 22*0e209d39SAndroid Build Coastguard Worker /** 23*0e209d39SAndroid Build Coastguard Worker * A class for representing a number to be processed by the decimal formatting pipeline. Includes 24*0e209d39SAndroid Build Coastguard Worker * methods for rounding, plural rules, and decimal digit extraction. 25*0e209d39SAndroid Build Coastguard Worker * 26*0e209d39SAndroid Build Coastguard Worker * <p>By design, this is NOT IMMUTABLE and NOT THREAD SAFE. It is intended to be an intermediate 27*0e209d39SAndroid Build Coastguard Worker * object holding state during a pass through the decimal formatting pipeline. 28*0e209d39SAndroid Build Coastguard Worker * 29*0e209d39SAndroid Build Coastguard Worker * <p>Represents numbers and digit display properties using Binary Coded Decimal (BCD). 30*0e209d39SAndroid Build Coastguard Worker * 31*0e209d39SAndroid Build Coastguard Worker * <p>Java has multiple implementations for testing, but C++ has only one implementation. 32*0e209d39SAndroid Build Coastguard Worker */ 33*0e209d39SAndroid Build Coastguard Worker class U_I18N_API DecimalQuantity : public IFixedDecimal, public UMemory { 34*0e209d39SAndroid Build Coastguard Worker public: 35*0e209d39SAndroid Build Coastguard Worker /** Copy constructor. */ 36*0e209d39SAndroid Build Coastguard Worker DecimalQuantity(const DecimalQuantity &other); 37*0e209d39SAndroid Build Coastguard Worker 38*0e209d39SAndroid Build Coastguard Worker /** Move constructor. */ 39*0e209d39SAndroid Build Coastguard Worker DecimalQuantity(DecimalQuantity &&src) noexcept; 40*0e209d39SAndroid Build Coastguard Worker 41*0e209d39SAndroid Build Coastguard Worker DecimalQuantity(); 42*0e209d39SAndroid Build Coastguard Worker 43*0e209d39SAndroid Build Coastguard Worker ~DecimalQuantity() override; 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker /** 46*0e209d39SAndroid Build Coastguard Worker * Sets this instance to be equal to another instance. 47*0e209d39SAndroid Build Coastguard Worker * 48*0e209d39SAndroid Build Coastguard Worker * @param other The instance to copy from. 49*0e209d39SAndroid Build Coastguard Worker */ 50*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &operator=(const DecimalQuantity &other); 51*0e209d39SAndroid Build Coastguard Worker 52*0e209d39SAndroid Build Coastguard Worker /** Move assignment */ 53*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &operator=(DecimalQuantity&& src) noexcept; 54*0e209d39SAndroid Build Coastguard Worker 55*0e209d39SAndroid Build Coastguard Worker /** 56*0e209d39SAndroid Build Coastguard Worker * If the minimum integer digits are greater than `minInt`, 57*0e209d39SAndroid Build Coastguard Worker * sets it to `minInt`. 58*0e209d39SAndroid Build Coastguard Worker * 59*0e209d39SAndroid Build Coastguard Worker * @param minInt The minimum number of integer digits. 60*0e209d39SAndroid Build Coastguard Worker */ 61*0e209d39SAndroid Build Coastguard Worker void decreaseMinIntegerTo(int32_t minInt); 62*0e209d39SAndroid Build Coastguard Worker 63*0e209d39SAndroid Build Coastguard Worker /** 64*0e209d39SAndroid Build Coastguard Worker * Sets the minimum integer digits that this {@link DecimalQuantity} should generate. 65*0e209d39SAndroid Build Coastguard Worker * This method does not perform rounding. 66*0e209d39SAndroid Build Coastguard Worker * 67*0e209d39SAndroid Build Coastguard Worker * @param minInt The minimum number of integer digits. 68*0e209d39SAndroid Build Coastguard Worker */ 69*0e209d39SAndroid Build Coastguard Worker void increaseMinIntegerTo(int32_t minInt); 70*0e209d39SAndroid Build Coastguard Worker 71*0e209d39SAndroid Build Coastguard Worker /** 72*0e209d39SAndroid Build Coastguard Worker * Sets the minimum fraction digits that this {@link DecimalQuantity} should generate. 73*0e209d39SAndroid Build Coastguard Worker * This method does not perform rounding. 74*0e209d39SAndroid Build Coastguard Worker * 75*0e209d39SAndroid Build Coastguard Worker * @param minFrac The minimum number of fraction digits. 76*0e209d39SAndroid Build Coastguard Worker */ 77*0e209d39SAndroid Build Coastguard Worker void setMinFraction(int32_t minFrac); 78*0e209d39SAndroid Build Coastguard Worker 79*0e209d39SAndroid Build Coastguard Worker /** 80*0e209d39SAndroid Build Coastguard Worker * Truncates digits from the upper magnitude of the number in order to satisfy the 81*0e209d39SAndroid Build Coastguard Worker * specified maximum number of integer digits. 82*0e209d39SAndroid Build Coastguard Worker * 83*0e209d39SAndroid Build Coastguard Worker * @param maxInt The maximum number of integer digits. 84*0e209d39SAndroid Build Coastguard Worker */ 85*0e209d39SAndroid Build Coastguard Worker void applyMaxInteger(int32_t maxInt); 86*0e209d39SAndroid Build Coastguard Worker 87*0e209d39SAndroid Build Coastguard Worker /** 88*0e209d39SAndroid Build Coastguard Worker * Rounds the number to a specified interval, such as 0.05. 89*0e209d39SAndroid Build Coastguard Worker * 90*0e209d39SAndroid Build Coastguard Worker * <p>If rounding to a power of ten, use the more efficient {@link #roundToMagnitude} instead. 91*0e209d39SAndroid Build Coastguard Worker * 92*0e209d39SAndroid Build Coastguard Worker * @param increment The increment to which to round. 93*0e209d39SAndroid Build Coastguard Worker * @param magnitude The power of 10 to which to round. 94*0e209d39SAndroid Build Coastguard Worker * @param roundingMode The {@link RoundingMode} to use if rounding is necessary. 95*0e209d39SAndroid Build Coastguard Worker */ 96*0e209d39SAndroid Build Coastguard Worker void roundToIncrement( 97*0e209d39SAndroid Build Coastguard Worker uint64_t increment, 98*0e209d39SAndroid Build Coastguard Worker digits_t magnitude, 99*0e209d39SAndroid Build Coastguard Worker RoundingMode roundingMode, 100*0e209d39SAndroid Build Coastguard Worker UErrorCode& status); 101*0e209d39SAndroid Build Coastguard Worker 102*0e209d39SAndroid Build Coastguard Worker /** Removes all fraction digits. */ 103*0e209d39SAndroid Build Coastguard Worker void truncate(); 104*0e209d39SAndroid Build Coastguard Worker 105*0e209d39SAndroid Build Coastguard Worker /** 106*0e209d39SAndroid Build Coastguard Worker * Rounds the number to the nearest multiple of 5 at the specified magnitude. 107*0e209d39SAndroid Build Coastguard Worker * For example, when magnitude == -2, this performs rounding to the nearest 0.05. 108*0e209d39SAndroid Build Coastguard Worker * 109*0e209d39SAndroid Build Coastguard Worker * @param magnitude The magnitude at which the digit should become either 0 or 5. 110*0e209d39SAndroid Build Coastguard Worker * @param roundingMode Rounding strategy. 111*0e209d39SAndroid Build Coastguard Worker */ 112*0e209d39SAndroid Build Coastguard Worker void roundToNickel(int32_t magnitude, RoundingMode roundingMode, UErrorCode& status); 113*0e209d39SAndroid Build Coastguard Worker 114*0e209d39SAndroid Build Coastguard Worker /** 115*0e209d39SAndroid Build Coastguard Worker * Rounds the number to a specified magnitude (power of ten). 116*0e209d39SAndroid Build Coastguard Worker * 117*0e209d39SAndroid Build Coastguard Worker * @param roundingMagnitude The power of ten to which to round. For example, a value of -2 will 118*0e209d39SAndroid Build Coastguard Worker * round to 2 decimal places. 119*0e209d39SAndroid Build Coastguard Worker * @param roundingMode The {@link RoundingMode} to use if rounding is necessary. 120*0e209d39SAndroid Build Coastguard Worker */ 121*0e209d39SAndroid Build Coastguard Worker void roundToMagnitude(int32_t magnitude, RoundingMode roundingMode, UErrorCode& status); 122*0e209d39SAndroid Build Coastguard Worker 123*0e209d39SAndroid Build Coastguard Worker /** 124*0e209d39SAndroid Build Coastguard Worker * Rounds the number to an infinite number of decimal points. This has no effect except for 125*0e209d39SAndroid Build Coastguard Worker * forcing the double in {@link DecimalQuantity_AbstractBCD} to adopt its exact representation. 126*0e209d39SAndroid Build Coastguard Worker */ 127*0e209d39SAndroid Build Coastguard Worker void roundToInfinity(); 128*0e209d39SAndroid Build Coastguard Worker 129*0e209d39SAndroid Build Coastguard Worker /** 130*0e209d39SAndroid Build Coastguard Worker * Multiply the internal value. Uses decNumber. 131*0e209d39SAndroid Build Coastguard Worker * 132*0e209d39SAndroid Build Coastguard Worker * @param multiplicand The value by which to multiply. 133*0e209d39SAndroid Build Coastguard Worker */ 134*0e209d39SAndroid Build Coastguard Worker void multiplyBy(const DecNum& multiplicand, UErrorCode& status); 135*0e209d39SAndroid Build Coastguard Worker 136*0e209d39SAndroid Build Coastguard Worker /** 137*0e209d39SAndroid Build Coastguard Worker * Divide the internal value. Uses decNumber. 138*0e209d39SAndroid Build Coastguard Worker * 139*0e209d39SAndroid Build Coastguard Worker * @param multiplicand The value by which to multiply. 140*0e209d39SAndroid Build Coastguard Worker */ 141*0e209d39SAndroid Build Coastguard Worker void divideBy(const DecNum& divisor, UErrorCode& status); 142*0e209d39SAndroid Build Coastguard Worker 143*0e209d39SAndroid Build Coastguard Worker /** Flips the sign from positive to negative and back. */ 144*0e209d39SAndroid Build Coastguard Worker void negate(); 145*0e209d39SAndroid Build Coastguard Worker 146*0e209d39SAndroid Build Coastguard Worker /** 147*0e209d39SAndroid Build Coastguard Worker * Scales the number by a power of ten. For example, if the value is currently "1234.56", calling 148*0e209d39SAndroid Build Coastguard Worker * this method with delta=-3 will change the value to "1.23456". 149*0e209d39SAndroid Build Coastguard Worker * 150*0e209d39SAndroid Build Coastguard Worker * @param delta The number of magnitudes of ten to change by. 151*0e209d39SAndroid Build Coastguard Worker * @return true if integer overflow occurred; false otherwise. 152*0e209d39SAndroid Build Coastguard Worker */ 153*0e209d39SAndroid Build Coastguard Worker bool adjustMagnitude(int32_t delta); 154*0e209d39SAndroid Build Coastguard Worker 155*0e209d39SAndroid Build Coastguard Worker /** 156*0e209d39SAndroid Build Coastguard Worker * Scales the number such that the least significant nonzero digit is at magnitude 0. 157*0e209d39SAndroid Build Coastguard Worker * 158*0e209d39SAndroid Build Coastguard Worker * @return The previous magnitude of the least significant digit. 159*0e209d39SAndroid Build Coastguard Worker */ 160*0e209d39SAndroid Build Coastguard Worker int32_t adjustToZeroScale(); 161*0e209d39SAndroid Build Coastguard Worker 162*0e209d39SAndroid Build Coastguard Worker /** 163*0e209d39SAndroid Build Coastguard Worker * @return The power of ten corresponding to the most significant nonzero digit. 164*0e209d39SAndroid Build Coastguard Worker * The number must not be zero. 165*0e209d39SAndroid Build Coastguard Worker */ 166*0e209d39SAndroid Build Coastguard Worker int32_t getMagnitude() const; 167*0e209d39SAndroid Build Coastguard Worker 168*0e209d39SAndroid Build Coastguard Worker /** 169*0e209d39SAndroid Build Coastguard Worker * @return The value of the (suppressed) exponent after the number has been 170*0e209d39SAndroid Build Coastguard Worker * put into a notation with exponents (ex: compact, scientific). Ex: given 171*0e209d39SAndroid Build Coastguard Worker * the number 1000 as "1K" / "1E3", the return value will be 3 (positive). 172*0e209d39SAndroid Build Coastguard Worker */ 173*0e209d39SAndroid Build Coastguard Worker int32_t getExponent() const; 174*0e209d39SAndroid Build Coastguard Worker 175*0e209d39SAndroid Build Coastguard Worker /** 176*0e209d39SAndroid Build Coastguard Worker * Adjusts the value for the (suppressed) exponent stored when using 177*0e209d39SAndroid Build Coastguard Worker * notation with exponents (ex: compact, scientific). 178*0e209d39SAndroid Build Coastguard Worker * 179*0e209d39SAndroid Build Coastguard Worker * <p>Adjusting the exponent is decoupled from {@link #adjustMagnitude} in 180*0e209d39SAndroid Build Coastguard Worker * order to allow flexibility for {@link StandardPlural} to be selected in 181*0e209d39SAndroid Build Coastguard Worker * formatting (ex: for compact notation) either with or without the exponent 182*0e209d39SAndroid Build Coastguard Worker * applied in the value of the number. 183*0e209d39SAndroid Build Coastguard Worker * @param delta 184*0e209d39SAndroid Build Coastguard Worker * The value to adjust the exponent by. 185*0e209d39SAndroid Build Coastguard Worker */ 186*0e209d39SAndroid Build Coastguard Worker void adjustExponent(int32_t delta); 187*0e209d39SAndroid Build Coastguard Worker 188*0e209d39SAndroid Build Coastguard Worker /** 189*0e209d39SAndroid Build Coastguard Worker * Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent. 190*0e209d39SAndroid Build Coastguard Worker */ 191*0e209d39SAndroid Build Coastguard Worker void resetExponent(); 192*0e209d39SAndroid Build Coastguard Worker 193*0e209d39SAndroid Build Coastguard Worker /** 194*0e209d39SAndroid Build Coastguard Worker * @return Whether the value represented by this {@link DecimalQuantity} is 195*0e209d39SAndroid Build Coastguard Worker * zero, infinity, or NaN. 196*0e209d39SAndroid Build Coastguard Worker */ 197*0e209d39SAndroid Build Coastguard Worker bool isZeroish() const; 198*0e209d39SAndroid Build Coastguard Worker 199*0e209d39SAndroid Build Coastguard Worker /** @return Whether the value represented by this {@link DecimalQuantity} is less than zero. */ 200*0e209d39SAndroid Build Coastguard Worker bool isNegative() const; 201*0e209d39SAndroid Build Coastguard Worker 202*0e209d39SAndroid Build Coastguard Worker /** @return The appropriate value from the Signum enum. */ 203*0e209d39SAndroid Build Coastguard Worker Signum signum() const; 204*0e209d39SAndroid Build Coastguard Worker 205*0e209d39SAndroid Build Coastguard Worker /** @return Whether the value represented by this {@link DecimalQuantity} is infinite. */ 206*0e209d39SAndroid Build Coastguard Worker bool isInfinite() const override; 207*0e209d39SAndroid Build Coastguard Worker 208*0e209d39SAndroid Build Coastguard Worker /** @return Whether the value represented by this {@link DecimalQuantity} is not a number. */ 209*0e209d39SAndroid Build Coastguard Worker bool isNaN() const override; 210*0e209d39SAndroid Build Coastguard Worker 211*0e209d39SAndroid Build Coastguard Worker /** 212*0e209d39SAndroid Build Coastguard Worker * Note: this method incorporates the value of {@code exponent} 213*0e209d39SAndroid Build Coastguard Worker * (for cases such as compact notation) to return the proper long value 214*0e209d39SAndroid Build Coastguard Worker * represented by the result. 215*0e209d39SAndroid Build Coastguard Worker * @param truncateIfOverflow if false and the number does NOT fit, fails with an assertion error. 216*0e209d39SAndroid Build Coastguard Worker */ 217*0e209d39SAndroid Build Coastguard Worker int64_t toLong(bool truncateIfOverflow = false) const; 218*0e209d39SAndroid Build Coastguard Worker 219*0e209d39SAndroid Build Coastguard Worker /** 220*0e209d39SAndroid Build Coastguard Worker * Note: this method incorporates the value of {@code exponent} 221*0e209d39SAndroid Build Coastguard Worker * (for cases such as compact notation) to return the proper long value 222*0e209d39SAndroid Build Coastguard Worker * represented by the result. 223*0e209d39SAndroid Build Coastguard Worker */ 224*0e209d39SAndroid Build Coastguard Worker uint64_t toFractionLong(bool includeTrailingZeros) const; 225*0e209d39SAndroid Build Coastguard Worker 226*0e209d39SAndroid Build Coastguard Worker /** 227*0e209d39SAndroid Build Coastguard Worker * Returns whether or not a Long can fully represent the value stored in this DecimalQuantity. 228*0e209d39SAndroid Build Coastguard Worker * @param ignoreFraction if true, silently ignore digits after the decimal place. 229*0e209d39SAndroid Build Coastguard Worker */ 230*0e209d39SAndroid Build Coastguard Worker bool fitsInLong(bool ignoreFraction = false) const; 231*0e209d39SAndroid Build Coastguard Worker 232*0e209d39SAndroid Build Coastguard Worker /** @return The value contained in this {@link DecimalQuantity} approximated as a double. */ 233*0e209d39SAndroid Build Coastguard Worker double toDouble() const; 234*0e209d39SAndroid Build Coastguard Worker 235*0e209d39SAndroid Build Coastguard Worker /** Computes a DecNum representation of this DecimalQuantity, saving it to the output parameter. */ 236*0e209d39SAndroid Build Coastguard Worker DecNum& toDecNum(DecNum& output, UErrorCode& status) const; 237*0e209d39SAndroid Build Coastguard Worker 238*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &setToInt(int32_t n); 239*0e209d39SAndroid Build Coastguard Worker 240*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &setToLong(int64_t n); 241*0e209d39SAndroid Build Coastguard Worker 242*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &setToDouble(double n); 243*0e209d39SAndroid Build Coastguard Worker 244*0e209d39SAndroid Build Coastguard Worker /** 245*0e209d39SAndroid Build Coastguard Worker * Produces a DecimalQuantity that was parsed from a string by the decNumber 246*0e209d39SAndroid Build Coastguard Worker * C Library. 247*0e209d39SAndroid Build Coastguard Worker * 248*0e209d39SAndroid Build Coastguard Worker * decNumber is similar to BigDecimal in Java, and supports parsing strings 249*0e209d39SAndroid Build Coastguard Worker * such as "123.456621E+40". 250*0e209d39SAndroid Build Coastguard Worker */ 251*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &setToDecNumber(StringPiece n, UErrorCode& status); 252*0e209d39SAndroid Build Coastguard Worker 253*0e209d39SAndroid Build Coastguard Worker /** Internal method if the caller already has a DecNum. */ 254*0e209d39SAndroid Build Coastguard Worker DecimalQuantity &setToDecNum(const DecNum& n, UErrorCode& status); 255*0e209d39SAndroid Build Coastguard Worker 256*0e209d39SAndroid Build Coastguard Worker /** Returns a DecimalQuantity after parsing the input string. */ 257*0e209d39SAndroid Build Coastguard Worker static DecimalQuantity fromExponentString(UnicodeString n, UErrorCode& status); 258*0e209d39SAndroid Build Coastguard Worker 259*0e209d39SAndroid Build Coastguard Worker /** 260*0e209d39SAndroid Build Coastguard Worker * Appends a digit, optionally with one or more leading zeros, to the end of the value represented 261*0e209d39SAndroid Build Coastguard Worker * by this DecimalQuantity. 262*0e209d39SAndroid Build Coastguard Worker * 263*0e209d39SAndroid Build Coastguard Worker * <p>The primary use of this method is to construct numbers during a parsing loop. It allows 264*0e209d39SAndroid Build Coastguard Worker * parsing to take advantage of the digit list infrastructure primarily designed for formatting. 265*0e209d39SAndroid Build Coastguard Worker * 266*0e209d39SAndroid Build Coastguard Worker * @param value The digit to append. 267*0e209d39SAndroid Build Coastguard Worker * @param leadingZeros The number of zeros to append before the digit. For example, if the value 268*0e209d39SAndroid Build Coastguard Worker * in this instance starts as 12.3, and you append a 4 with 1 leading zero, the value becomes 269*0e209d39SAndroid Build Coastguard Worker * 12.304. 270*0e209d39SAndroid Build Coastguard Worker * @param appendAsInteger If true, increase the magnitude of existing digits to make room for the 271*0e209d39SAndroid Build Coastguard Worker * new digit. If false, append to the end like a fraction digit. If true, there must not be 272*0e209d39SAndroid Build Coastguard Worker * any fraction digits already in the number. 273*0e209d39SAndroid Build Coastguard Worker * @internal 274*0e209d39SAndroid Build Coastguard Worker * @deprecated This API is ICU internal only. 275*0e209d39SAndroid Build Coastguard Worker */ 276*0e209d39SAndroid Build Coastguard Worker void appendDigit(int8_t value, int32_t leadingZeros, bool appendAsInteger); 277*0e209d39SAndroid Build Coastguard Worker 278*0e209d39SAndroid Build Coastguard Worker double getPluralOperand(PluralOperand operand) const override; 279*0e209d39SAndroid Build Coastguard Worker 280*0e209d39SAndroid Build Coastguard Worker bool hasIntegerValue() const override; 281*0e209d39SAndroid Build Coastguard Worker 282*0e209d39SAndroid Build Coastguard Worker /** 283*0e209d39SAndroid Build Coastguard Worker * Gets the digit at the specified magnitude. For example, if the represented number is 12.3, 284*0e209d39SAndroid Build Coastguard Worker * getDigit(-1) returns 3, since 3 is the digit corresponding to 10^-1. 285*0e209d39SAndroid Build Coastguard Worker * 286*0e209d39SAndroid Build Coastguard Worker * @param magnitude The magnitude of the digit. 287*0e209d39SAndroid Build Coastguard Worker * @return The digit at the specified magnitude. 288*0e209d39SAndroid Build Coastguard Worker */ 289*0e209d39SAndroid Build Coastguard Worker int8_t getDigit(int32_t magnitude) const; 290*0e209d39SAndroid Build Coastguard Worker 291*0e209d39SAndroid Build Coastguard Worker /** 292*0e209d39SAndroid Build Coastguard Worker * Gets the largest power of ten that needs to be displayed. The value returned by this function 293*0e209d39SAndroid Build Coastguard Worker * will be bounded between minInt and maxInt. 294*0e209d39SAndroid Build Coastguard Worker * 295*0e209d39SAndroid Build Coastguard Worker * @return The highest-magnitude digit to be displayed. 296*0e209d39SAndroid Build Coastguard Worker */ 297*0e209d39SAndroid Build Coastguard Worker int32_t getUpperDisplayMagnitude() const; 298*0e209d39SAndroid Build Coastguard Worker 299*0e209d39SAndroid Build Coastguard Worker /** 300*0e209d39SAndroid Build Coastguard Worker * Gets the smallest power of ten that needs to be displayed. The value returned by this function 301*0e209d39SAndroid Build Coastguard Worker * will be bounded between -minFrac and -maxFrac. 302*0e209d39SAndroid Build Coastguard Worker * 303*0e209d39SAndroid Build Coastguard Worker * @return The lowest-magnitude digit to be displayed. 304*0e209d39SAndroid Build Coastguard Worker */ 305*0e209d39SAndroid Build Coastguard Worker int32_t getLowerDisplayMagnitude() const; 306*0e209d39SAndroid Build Coastguard Worker 307*0e209d39SAndroid Build Coastguard Worker int32_t fractionCount() const; 308*0e209d39SAndroid Build Coastguard Worker 309*0e209d39SAndroid Build Coastguard Worker int32_t fractionCountWithoutTrailingZeros() const; 310*0e209d39SAndroid Build Coastguard Worker 311*0e209d39SAndroid Build Coastguard Worker void clear(); 312*0e209d39SAndroid Build Coastguard Worker 313*0e209d39SAndroid Build Coastguard Worker /** This method is for internal testing only. */ 314*0e209d39SAndroid Build Coastguard Worker uint64_t getPositionFingerprint() const; 315*0e209d39SAndroid Build Coastguard Worker 316*0e209d39SAndroid Build Coastguard Worker // /** 317*0e209d39SAndroid Build Coastguard Worker // * If the given {@link FieldPosition} is a {@link UFieldPosition}, populates it with the fraction 318*0e209d39SAndroid Build Coastguard Worker // * length and fraction long value. If the argument is not a {@link UFieldPosition}, nothing 319*0e209d39SAndroid Build Coastguard Worker // * happens. 320*0e209d39SAndroid Build Coastguard Worker // * 321*0e209d39SAndroid Build Coastguard Worker // * @param fp The {@link UFieldPosition} to populate. 322*0e209d39SAndroid Build Coastguard Worker // */ 323*0e209d39SAndroid Build Coastguard Worker // void populateUFieldPosition(FieldPosition fp); 324*0e209d39SAndroid Build Coastguard Worker 325*0e209d39SAndroid Build Coastguard Worker /** 326*0e209d39SAndroid Build Coastguard Worker * Checks whether the bytes stored in this instance are all valid. For internal unit testing only. 327*0e209d39SAndroid Build Coastguard Worker * 328*0e209d39SAndroid Build Coastguard Worker * @return An error message if this instance is invalid, or null if this instance is healthy. 329*0e209d39SAndroid Build Coastguard Worker */ 330*0e209d39SAndroid Build Coastguard Worker const char16_t* checkHealth() const; 331*0e209d39SAndroid Build Coastguard Worker 332*0e209d39SAndroid Build Coastguard Worker UnicodeString toString() const; 333*0e209d39SAndroid Build Coastguard Worker 334*0e209d39SAndroid Build Coastguard Worker /** Returns the string in standard exponential notation. */ 335*0e209d39SAndroid Build Coastguard Worker UnicodeString toScientificString() const; 336*0e209d39SAndroid Build Coastguard Worker 337*0e209d39SAndroid Build Coastguard Worker /** Returns the string without exponential notation. Slightly slower than toScientificString(). */ 338*0e209d39SAndroid Build Coastguard Worker UnicodeString toPlainString() const; 339*0e209d39SAndroid Build Coastguard Worker 340*0e209d39SAndroid Build Coastguard Worker /** Returns the string using ASCII digits and using exponential notation for non-zero 341*0e209d39SAndroid Build Coastguard Worker exponents, following the UTS 35 specification for plural rule samples. */ 342*0e209d39SAndroid Build Coastguard Worker UnicodeString toExponentString() const; 343*0e209d39SAndroid Build Coastguard Worker 344*0e209d39SAndroid Build Coastguard Worker /** Visible for testing */ isUsingBytes()345*0e209d39SAndroid Build Coastguard Worker inline bool isUsingBytes() { return usingBytes; } 346*0e209d39SAndroid Build Coastguard Worker 347*0e209d39SAndroid Build Coastguard Worker /** Visible for testing */ isExplicitExactDouble()348*0e209d39SAndroid Build Coastguard Worker inline bool isExplicitExactDouble() { return explicitExactDouble; } 349*0e209d39SAndroid Build Coastguard Worker 350*0e209d39SAndroid Build Coastguard Worker bool operator==(const DecimalQuantity& other) const; 351*0e209d39SAndroid Build Coastguard Worker 352*0e209d39SAndroid Build Coastguard Worker inline bool operator!=(const DecimalQuantity& other) const { 353*0e209d39SAndroid Build Coastguard Worker return !(*this == other); 354*0e209d39SAndroid Build Coastguard Worker } 355*0e209d39SAndroid Build Coastguard Worker 356*0e209d39SAndroid Build Coastguard Worker /** 357*0e209d39SAndroid Build Coastguard Worker * Bogus flag for when a DecimalQuantity is stored on the stack. 358*0e209d39SAndroid Build Coastguard Worker */ 359*0e209d39SAndroid Build Coastguard Worker bool bogus = false; 360*0e209d39SAndroid Build Coastguard Worker 361*0e209d39SAndroid Build Coastguard Worker private: 362*0e209d39SAndroid Build Coastguard Worker /** 363*0e209d39SAndroid Build Coastguard Worker * The power of ten corresponding to the least significant digit in the BCD. For example, if this 364*0e209d39SAndroid Build Coastguard Worker * object represents the number "3.14", the BCD will be "0x314" and the scale will be -2. 365*0e209d39SAndroid Build Coastguard Worker * 366*0e209d39SAndroid Build Coastguard Worker * <p>Note that in {@link java.math.BigDecimal}, the scale is defined differently: the number of 367*0e209d39SAndroid Build Coastguard Worker * digits after the decimal place, which is the negative of our definition of scale. 368*0e209d39SAndroid Build Coastguard Worker */ 369*0e209d39SAndroid Build Coastguard Worker int32_t scale; 370*0e209d39SAndroid Build Coastguard Worker 371*0e209d39SAndroid Build Coastguard Worker /** 372*0e209d39SAndroid Build Coastguard Worker * The number of digits in the BCD. For example, "1007" has BCD "0x1007" and precision 4. The 373*0e209d39SAndroid Build Coastguard Worker * maximum precision is 16 since a long can hold only 16 digits. 374*0e209d39SAndroid Build Coastguard Worker * 375*0e209d39SAndroid Build Coastguard Worker * <p>This value must be re-calculated whenever the value in bcd changes by using {@link 376*0e209d39SAndroid Build Coastguard Worker * #computePrecisionAndCompact()}. 377*0e209d39SAndroid Build Coastguard Worker */ 378*0e209d39SAndroid Build Coastguard Worker int32_t precision; 379*0e209d39SAndroid Build Coastguard Worker 380*0e209d39SAndroid Build Coastguard Worker /** 381*0e209d39SAndroid Build Coastguard Worker * A bitmask of properties relating to the number represented by this object. 382*0e209d39SAndroid Build Coastguard Worker * 383*0e209d39SAndroid Build Coastguard Worker * @see #NEGATIVE_FLAG 384*0e209d39SAndroid Build Coastguard Worker * @see #INFINITY_FLAG 385*0e209d39SAndroid Build Coastguard Worker * @see #NAN_FLAG 386*0e209d39SAndroid Build Coastguard Worker */ 387*0e209d39SAndroid Build Coastguard Worker int8_t flags; 388*0e209d39SAndroid Build Coastguard Worker 389*0e209d39SAndroid Build Coastguard Worker // The following three fields relate to the double-to-ascii fast path algorithm. 390*0e209d39SAndroid Build Coastguard Worker // When a double is given to DecimalQuantityBCD, it is converted to using a fast algorithm. The 391*0e209d39SAndroid Build Coastguard Worker // fast algorithm guarantees correctness to only the first ~12 digits of the double. The process 392*0e209d39SAndroid Build Coastguard Worker // of rounding the number ensures that the converted digits are correct, falling back to a slow- 393*0e209d39SAndroid Build Coastguard Worker // path algorithm if required. Therefore, if a DecimalQuantity is constructed from a double, it 394*0e209d39SAndroid Build Coastguard Worker // is *required* that roundToMagnitude(), roundToIncrement(), or roundToInfinity() is called. If 395*0e209d39SAndroid Build Coastguard Worker // you don't round, assertions will fail in certain other methods if you try calling them. 396*0e209d39SAndroid Build Coastguard Worker 397*0e209d39SAndroid Build Coastguard Worker /** 398*0e209d39SAndroid Build Coastguard Worker * Whether the value in the BCD comes from the double fast path without having been rounded to 399*0e209d39SAndroid Build Coastguard Worker * ensure correctness 400*0e209d39SAndroid Build Coastguard Worker */ 401*0e209d39SAndroid Build Coastguard Worker UBool isApproximate; 402*0e209d39SAndroid Build Coastguard Worker 403*0e209d39SAndroid Build Coastguard Worker /** 404*0e209d39SAndroid Build Coastguard Worker * The original number provided by the user and which is represented in BCD. Used when we need to 405*0e209d39SAndroid Build Coastguard Worker * re-compute the BCD for an exact double representation. 406*0e209d39SAndroid Build Coastguard Worker */ 407*0e209d39SAndroid Build Coastguard Worker double origDouble; 408*0e209d39SAndroid Build Coastguard Worker 409*0e209d39SAndroid Build Coastguard Worker /** 410*0e209d39SAndroid Build Coastguard Worker * The change in magnitude relative to the original double. Used when we need to re-compute the 411*0e209d39SAndroid Build Coastguard Worker * BCD for an exact double representation. 412*0e209d39SAndroid Build Coastguard Worker */ 413*0e209d39SAndroid Build Coastguard Worker int32_t origDelta; 414*0e209d39SAndroid Build Coastguard Worker 415*0e209d39SAndroid Build Coastguard Worker // Positions to keep track of leading and trailing zeros. 416*0e209d39SAndroid Build Coastguard Worker // lReqPos is the magnitude of the first required leading zero. 417*0e209d39SAndroid Build Coastguard Worker // rReqPos is the magnitude of the last required trailing zero. 418*0e209d39SAndroid Build Coastguard Worker int32_t lReqPos = 0; 419*0e209d39SAndroid Build Coastguard Worker int32_t rReqPos = 0; 420*0e209d39SAndroid Build Coastguard Worker 421*0e209d39SAndroid Build Coastguard Worker // The value of the (suppressed) exponent after the number has been put into 422*0e209d39SAndroid Build Coastguard Worker // a notation with exponents (ex: compact, scientific). 423*0e209d39SAndroid Build Coastguard Worker int32_t exponent = 0; 424*0e209d39SAndroid Build Coastguard Worker 425*0e209d39SAndroid Build Coastguard Worker /** 426*0e209d39SAndroid Build Coastguard Worker * The BCD of the 16 digits of the number represented by this object. Every 4 bits of the long map 427*0e209d39SAndroid Build Coastguard Worker * to one digit. For example, the number "12345" in BCD is "0x12345". 428*0e209d39SAndroid Build Coastguard Worker * 429*0e209d39SAndroid Build Coastguard Worker * <p>Whenever bcd changes internally, {@link #compact()} must be called, except in special cases 430*0e209d39SAndroid Build Coastguard Worker * like setting the digit to zero. 431*0e209d39SAndroid Build Coastguard Worker */ 432*0e209d39SAndroid Build Coastguard Worker union { 433*0e209d39SAndroid Build Coastguard Worker struct { 434*0e209d39SAndroid Build Coastguard Worker int8_t *ptr; 435*0e209d39SAndroid Build Coastguard Worker int32_t len; 436*0e209d39SAndroid Build Coastguard Worker } bcdBytes; 437*0e209d39SAndroid Build Coastguard Worker uint64_t bcdLong; 438*0e209d39SAndroid Build Coastguard Worker } fBCD; 439*0e209d39SAndroid Build Coastguard Worker 440*0e209d39SAndroid Build Coastguard Worker bool usingBytes = false; 441*0e209d39SAndroid Build Coastguard Worker 442*0e209d39SAndroid Build Coastguard Worker /** 443*0e209d39SAndroid Build Coastguard Worker * Whether this {@link DecimalQuantity} has been explicitly converted to an exact double. true if 444*0e209d39SAndroid Build Coastguard Worker * backed by a double that was explicitly converted via convertToAccurateDouble; false otherwise. 445*0e209d39SAndroid Build Coastguard Worker * Used for testing. 446*0e209d39SAndroid Build Coastguard Worker */ 447*0e209d39SAndroid Build Coastguard Worker bool explicitExactDouble = false; 448*0e209d39SAndroid Build Coastguard Worker 449*0e209d39SAndroid Build Coastguard Worker void roundToMagnitude(int32_t magnitude, RoundingMode roundingMode, bool nickel, UErrorCode& status); 450*0e209d39SAndroid Build Coastguard Worker 451*0e209d39SAndroid Build Coastguard Worker /** 452*0e209d39SAndroid Build Coastguard Worker * Returns a single digit from the BCD list. No internal state is changed by calling this method. 453*0e209d39SAndroid Build Coastguard Worker * 454*0e209d39SAndroid Build Coastguard Worker * @param position The position of the digit to pop, counted in BCD units from the least 455*0e209d39SAndroid Build Coastguard Worker * significant digit. If outside the range supported by the implementation, zero is returned. 456*0e209d39SAndroid Build Coastguard Worker * @return The digit at the specified location. 457*0e209d39SAndroid Build Coastguard Worker */ 458*0e209d39SAndroid Build Coastguard Worker int8_t getDigitPos(int32_t position) const; 459*0e209d39SAndroid Build Coastguard Worker 460*0e209d39SAndroid Build Coastguard Worker /** 461*0e209d39SAndroid Build Coastguard Worker * Sets the digit in the BCD list. This method only sets the digit; it is the caller's 462*0e209d39SAndroid Build Coastguard Worker * responsibility to call {@link #compact} after setting the digit, and to ensure 463*0e209d39SAndroid Build Coastguard Worker * that the precision field is updated to reflect the correct number of digits if a 464*0e209d39SAndroid Build Coastguard Worker * nonzero digit is added to the decimal. 465*0e209d39SAndroid Build Coastguard Worker * 466*0e209d39SAndroid Build Coastguard Worker * @param position The position of the digit to pop, counted in BCD units from the least 467*0e209d39SAndroid Build Coastguard Worker * significant digit. If outside the range supported by the implementation, an AssertionError 468*0e209d39SAndroid Build Coastguard Worker * is thrown. 469*0e209d39SAndroid Build Coastguard Worker * @param value The digit to set at the specified location. 470*0e209d39SAndroid Build Coastguard Worker */ 471*0e209d39SAndroid Build Coastguard Worker void setDigitPos(int32_t position, int8_t value); 472*0e209d39SAndroid Build Coastguard Worker 473*0e209d39SAndroid Build Coastguard Worker /** 474*0e209d39SAndroid Build Coastguard Worker * Adds zeros to the end of the BCD list. This will result in an invalid BCD representation; it is 475*0e209d39SAndroid Build Coastguard Worker * the caller's responsibility to do further manipulation and then call {@link #compact}. 476*0e209d39SAndroid Build Coastguard Worker * 477*0e209d39SAndroid Build Coastguard Worker * @param numDigits The number of zeros to add. 478*0e209d39SAndroid Build Coastguard Worker */ 479*0e209d39SAndroid Build Coastguard Worker void shiftLeft(int32_t numDigits); 480*0e209d39SAndroid Build Coastguard Worker 481*0e209d39SAndroid Build Coastguard Worker /** 482*0e209d39SAndroid Build Coastguard Worker * Directly removes digits from the end of the BCD list. 483*0e209d39SAndroid Build Coastguard Worker * Updates the scale and precision. 484*0e209d39SAndroid Build Coastguard Worker * 485*0e209d39SAndroid Build Coastguard Worker * CAUTION: it is the caller's responsibility to call {@link #compact} after this method. 486*0e209d39SAndroid Build Coastguard Worker */ 487*0e209d39SAndroid Build Coastguard Worker void shiftRight(int32_t numDigits); 488*0e209d39SAndroid Build Coastguard Worker 489*0e209d39SAndroid Build Coastguard Worker /** 490*0e209d39SAndroid Build Coastguard Worker * Directly removes digits from the front of the BCD list. 491*0e209d39SAndroid Build Coastguard Worker * Updates precision. 492*0e209d39SAndroid Build Coastguard Worker * 493*0e209d39SAndroid Build Coastguard Worker * CAUTION: it is the caller's responsibility to call {@link #compact} after this method. 494*0e209d39SAndroid Build Coastguard Worker */ 495*0e209d39SAndroid Build Coastguard Worker void popFromLeft(int32_t numDigits); 496*0e209d39SAndroid Build Coastguard Worker 497*0e209d39SAndroid Build Coastguard Worker /** 498*0e209d39SAndroid Build Coastguard Worker * Sets the internal representation to zero. Clears any values stored in scale, precision, 499*0e209d39SAndroid Build Coastguard Worker * hasDouble, origDouble, origDelta, exponent, and BCD data. 500*0e209d39SAndroid Build Coastguard Worker */ 501*0e209d39SAndroid Build Coastguard Worker void setBcdToZero(); 502*0e209d39SAndroid Build Coastguard Worker 503*0e209d39SAndroid Build Coastguard Worker /** 504*0e209d39SAndroid Build Coastguard Worker * Sets the internal BCD state to represent the value in the given int. The int is guaranteed to 505*0e209d39SAndroid Build Coastguard Worker * be either positive. The internal state is guaranteed to be empty when this method is called. 506*0e209d39SAndroid Build Coastguard Worker * 507*0e209d39SAndroid Build Coastguard Worker * @param n The value to consume. 508*0e209d39SAndroid Build Coastguard Worker */ 509*0e209d39SAndroid Build Coastguard Worker void readIntToBcd(int32_t n); 510*0e209d39SAndroid Build Coastguard Worker 511*0e209d39SAndroid Build Coastguard Worker /** 512*0e209d39SAndroid Build Coastguard Worker * Sets the internal BCD state to represent the value in the given long. The long is guaranteed to 513*0e209d39SAndroid Build Coastguard Worker * be either positive. The internal state is guaranteed to be empty when this method is called. 514*0e209d39SAndroid Build Coastguard Worker * 515*0e209d39SAndroid Build Coastguard Worker * @param n The value to consume. 516*0e209d39SAndroid Build Coastguard Worker */ 517*0e209d39SAndroid Build Coastguard Worker void readLongToBcd(int64_t n); 518*0e209d39SAndroid Build Coastguard Worker 519*0e209d39SAndroid Build Coastguard Worker void readDecNumberToBcd(const DecNum& dn); 520*0e209d39SAndroid Build Coastguard Worker 521*0e209d39SAndroid Build Coastguard Worker void readDoubleConversionToBcd(const char* buffer, int32_t length, int32_t point); 522*0e209d39SAndroid Build Coastguard Worker 523*0e209d39SAndroid Build Coastguard Worker void copyFieldsFrom(const DecimalQuantity& other); 524*0e209d39SAndroid Build Coastguard Worker 525*0e209d39SAndroid Build Coastguard Worker void copyBcdFrom(const DecimalQuantity &other); 526*0e209d39SAndroid Build Coastguard Worker 527*0e209d39SAndroid Build Coastguard Worker void moveBcdFrom(DecimalQuantity& src); 528*0e209d39SAndroid Build Coastguard Worker 529*0e209d39SAndroid Build Coastguard Worker /** 530*0e209d39SAndroid Build Coastguard Worker * Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the 531*0e209d39SAndroid Build Coastguard Worker * precision. The precision is the number of digits in the number up through the greatest nonzero 532*0e209d39SAndroid Build Coastguard Worker * digit. 533*0e209d39SAndroid Build Coastguard Worker * 534*0e209d39SAndroid Build Coastguard Worker * <p>This method must always be called when bcd changes in order for assumptions to be correct in 535*0e209d39SAndroid Build Coastguard Worker * methods like {@link #fractionCount()}. 536*0e209d39SAndroid Build Coastguard Worker */ 537*0e209d39SAndroid Build Coastguard Worker void compact(); 538*0e209d39SAndroid Build Coastguard Worker 539*0e209d39SAndroid Build Coastguard Worker void _setToInt(int32_t n); 540*0e209d39SAndroid Build Coastguard Worker 541*0e209d39SAndroid Build Coastguard Worker void _setToLong(int64_t n); 542*0e209d39SAndroid Build Coastguard Worker 543*0e209d39SAndroid Build Coastguard Worker void _setToDoubleFast(double n); 544*0e209d39SAndroid Build Coastguard Worker 545*0e209d39SAndroid Build Coastguard Worker void _setToDecNum(const DecNum& dn, UErrorCode& status); 546*0e209d39SAndroid Build Coastguard Worker 547*0e209d39SAndroid Build Coastguard Worker static int32_t getVisibleFractionCount(UnicodeString value); 548*0e209d39SAndroid Build Coastguard Worker 549*0e209d39SAndroid Build Coastguard Worker void convertToAccurateDouble(); 550*0e209d39SAndroid Build Coastguard Worker 551*0e209d39SAndroid Build Coastguard Worker /** Ensure that a byte array of at least 40 digits is allocated. */ 552*0e209d39SAndroid Build Coastguard Worker void ensureCapacity(); 553*0e209d39SAndroid Build Coastguard Worker 554*0e209d39SAndroid Build Coastguard Worker void ensureCapacity(int32_t capacity); 555*0e209d39SAndroid Build Coastguard Worker 556*0e209d39SAndroid Build Coastguard Worker /** Switches the internal storage mechanism between the 64-bit long and the byte array. */ 557*0e209d39SAndroid Build Coastguard Worker void switchStorage(); 558*0e209d39SAndroid Build Coastguard Worker }; 559*0e209d39SAndroid Build Coastguard Worker 560*0e209d39SAndroid Build Coastguard Worker } // namespace number::impl 561*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 562*0e209d39SAndroid Build Coastguard Worker 563*0e209d39SAndroid Build Coastguard Worker 564*0e209d39SAndroid Build Coastguard Worker #endif //__NUMBER_DECIMALQUANTITY_H__ 565*0e209d39SAndroid Build Coastguard Worker 566*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_FORMATTING */ 567