2 **********************************************************************
3 * Copyright (C) 1997-2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 * Modification History:
11 * Date Name Description
12 * 03/21/97 clhuang Converted from java.
13 * 03/21/97 clhuang Implemented with new APIs.
14 * 03/27/97 helena Updated to pass the simple test after code review.
15 * 03/31/97 aliu Moved isLONG_MIN to here, and fixed it.
16 * 04/15/97 aliu Changed MAX_COUNT to DBL_DIG. Changed Digit to char.
17 * Reworked representation by replacing fDecimalAt
19 * 04/16/97 aliu Rewrote set() and getDouble() to use sprintf/atof
20 * to do digit conversion.
21 * 09/09/97 aliu Modified for exponential notation support.
22 * 08/02/98 stephen Added nearest/even rounding
23 * Fixed bug in fitsIntoLong
24 ******************************************************************************
29 #if !UCONFIG_NO_FORMATTING
30 #include "unicode/putil.h"
37 #include "digitinterval.h"
44 // ***************************************************************************
46 // A wrapper onto decNumber.
47 // Used to be standalone.
48 // ***************************************************************************
51 * This is the zero digit. The base for the digits returned by getDigit()
52 * Note that it is the platform invariant digit, and is not Unicode.
57 /* Only for 32 bit numbers. Ignore the negative sign. */
58 //static const char LONG_MIN_REP[] = "2147483648";
59 //static const char I64_MIN_REP[] = "9223372036854775808";
64 // -------------------------------------
65 // default constructor
67 DigitList::DigitList()
69 uprv_decContextDefault(&fContext
, DEC_INIT_BASE
);
71 uprv_decContextSetRounding(&fContext
, DEC_ROUND_HALF_EVEN
);
72 fContext
.digits
= fStorage
.getCapacity();
74 fDecNumber
= fStorage
.getAlias();
75 uprv_decNumberZero(fDecNumber
);
77 internalSetDouble(0.0);
80 // -------------------------------------
82 DigitList::~DigitList()
86 // -------------------------------------
89 DigitList::DigitList(const DigitList
&other
)
91 fDecNumber
= fStorage
.getAlias();
96 // -------------------------------------
97 // assignment operator
100 DigitList::operator=(const DigitList
& other
)
104 uprv_memcpy(&fContext
, &other
.fContext
, sizeof(decContext
));
106 if (other
.fStorage
.getCapacity() > fStorage
.getCapacity()) {
107 fDecNumber
= fStorage
.resize(other
.fStorage
.getCapacity());
109 // Always reset the fContext.digits, even if fDecNumber was not reallocated,
110 // because above we copied fContext from other.fContext.
111 fContext
.digits
= fStorage
.getCapacity();
112 uprv_decNumberCopy(fDecNumber
, other
.fDecNumber
);
115 // fDouble is lazily created and cached.
116 // Avoid potential races with that happening with other.fDouble
117 // while we are doing the assignment.
120 if(other
.fHave
==kDouble
) {
121 fUnion
.fDouble
= other
.fUnion
.fDouble
;
129 // -------------------------------------
130 // operator == (does not exactly match the old DigitList function)
133 DigitList::operator==(const DigitList
& that
) const
138 decNumber n
; // Has space for only a none digit value.
140 uprv_decContextDefault(&c
, DEC_INIT_BASE
);
144 uprv_decNumberCompare(&n
, this->fDecNumber
, that
.fDecNumber
, &c
);
145 UBool result
= decNumberIsZero(&n
);
149 // -------------------------------------
150 // comparison function. Returns
151 // Not Comparable : -2
155 int32_t DigitList::compare(const DigitList
&other
) {
157 int32_t savedDigits
= fContext
.digits
;
159 uprv_decNumberCompare(&result
, this->fDecNumber
, other
.fDecNumber
, &fContext
);
160 fContext
.digits
= savedDigits
;
161 if (decNumberIsZero(&result
)) {
163 } else if (decNumberIsSpecial(&result
)) {
165 } else if (result
.bits
& DECNEG
) {
173 // -------------------------------------
174 // Reduce - remove trailing zero digits.
176 DigitList::reduce() {
177 uprv_decNumberReduce(fDecNumber
, fDecNumber
, &fContext
);
181 // -------------------------------------
182 // trim - remove trailing fraction zero digits.
185 uprv_decNumberTrim(fDecNumber
);
188 // -------------------------------------
189 // Resets the digit list; sets all the digits to zero.
194 uprv_decNumberZero(fDecNumber
);
195 uprv_decContextSetRounding(&fContext
, DEC_ROUND_HALF_EVEN
);
196 internalSetDouble(0.0);
201 * Formats a int64_t number into a base 10 string representation, and NULL terminates it.
202 * @param number The number to format
203 * @param outputStr The string to output to. Must be at least MAX_DIGITS+2 in length (21),
204 * to hold the longest int64_t value.
205 * @return the number of digits written, not including the sign.
208 formatBase10(int64_t number
, char *outputStr
) {
209 // The number is output backwards, starting with the LSD.
210 // Fill the buffer from the far end. After the number is complete,
211 // slide the string contents to the front.
213 const int32_t MAX_IDX
= MAX_DIGITS
+2;
214 int32_t destIdx
= MAX_IDX
;
215 outputStr
[--destIdx
] = 0;
218 if (number
< 0) { // Negative numbers are slightly larger than a postive
219 outputStr
[--destIdx
] = (char)(-(n
% 10) + kZero
);
223 outputStr
[--destIdx
] = (char)(n
% 10 + kZero
);
228 outputStr
[--destIdx
] = '-';
231 // Slide the number to the start of the output str
232 U_ASSERT(destIdx
>= 0);
233 int32_t length
= MAX_IDX
- destIdx
;
234 uprv_memmove(outputStr
, outputStr
+MAX_IDX
-length
, length
);
240 // -------------------------------------
243 // For most modes, the meaning and names are the same between the decNumber library
244 // (which DigitList follows) and the ICU Formatting Rounding Mode values.
245 // The flag constants are different, however.
247 // Note that ICU's kRoundingUnnecessary is not implemented directly by DigitList.
248 // This mode, inherited from Java, means that numbers that would not format exactly
249 // will return an error when formatting is attempted.
252 DigitList::setRoundingMode(DecimalFormat::ERoundingMode m
) {
256 case DecimalFormat::kRoundCeiling
: r
= DEC_ROUND_CEILING
; break;
257 case DecimalFormat::kRoundFloor
: r
= DEC_ROUND_FLOOR
; break;
258 case DecimalFormat::kRoundDown
: r
= DEC_ROUND_DOWN
; break;
259 case DecimalFormat::kRoundUp
: r
= DEC_ROUND_UP
; break;
260 case DecimalFormat::kRoundHalfEven
: r
= DEC_ROUND_HALF_EVEN
; break;
261 case DecimalFormat::kRoundHalfDown
: r
= DEC_ROUND_HALF_DOWN
; break;
262 case DecimalFormat::kRoundHalfUp
: r
= DEC_ROUND_HALF_UP
; break;
263 case DecimalFormat::kRoundUnnecessary
: r
= DEC_ROUND_HALF_EVEN
; break;
265 // TODO: how to report the problem?
266 // Leave existing mode unchanged.
267 r
= uprv_decContextGetRounding(&fContext
);
269 uprv_decContextSetRounding(&fContext
, r
);
274 // -------------------------------------
277 DigitList::setPositive(UBool s
) {
279 fDecNumber
->bits
&= ~DECNEG
;
281 fDecNumber
->bits
|= DECNEG
;
285 // -------------------------------------
288 DigitList::setDecimalAt(int32_t d
) {
289 U_ASSERT((fDecNumber
->bits
& DECSPECIAL
) == 0); // Not Infinity or NaN
290 U_ASSERT(d
-1>-999999999);
291 U_ASSERT(d
-1< 999999999);
292 int32_t adjustedDigits
= fDecNumber
->digits
;
293 if (decNumberIsZero(fDecNumber
)) {
294 // Account for difference in how zero is represented between DigitList & decNumber.
297 fDecNumber
->exponent
= d
- adjustedDigits
;
302 DigitList::getDecimalAt() {
303 U_ASSERT((fDecNumber
->bits
& DECSPECIAL
) == 0); // Not Infinity or NaN
304 if (decNumberIsZero(fDecNumber
) || ((fDecNumber
->bits
& DECSPECIAL
) != 0)) {
305 return fDecNumber
->exponent
; // Exponent should be zero for these cases.
307 return fDecNumber
->exponent
+ fDecNumber
->digits
;
311 DigitList::setCount(int32_t c
) {
312 U_ASSERT(c
<= fContext
.digits
);
314 // For a value of zero, DigitList sets all fields to zero, while
315 // decNumber keeps one digit (with that digit being a zero)
317 fDecNumber
->lsu
[0] = 0;
319 fDecNumber
->digits
= c
;
324 DigitList::getCount() const {
325 if (decNumberIsZero(fDecNumber
) && fDecNumber
->exponent
==0) {
326 // The extra test for exponent==0 is needed because parsing sometimes appends
327 // zero digits. It's bogus, decimalFormatter parsing needs to be cleaned up.
330 return fDecNumber
->digits
;
335 DigitList::setDigit(int32_t i
, char v
) {
336 int32_t count
= fDecNumber
->digits
;
338 U_ASSERT(v
>='0' && v
<='9');
340 fDecNumber
->lsu
[count
-i
-1] = v
;
345 DigitList::getDigit(int32_t i
) {
346 int32_t count
= fDecNumber
->digits
;
348 return fDecNumber
->lsu
[count
-i
-1] + '0';
351 // copied from DigitList::getDigit()
353 DigitList::getDigitValue(int32_t i
) {
354 int32_t count
= fDecNumber
->digits
;
356 return fDecNumber
->lsu
[count
-i
-1];
359 // -------------------------------------
360 // Appends the digit to the digit list if it's not out of scope.
361 // Ignores the digit, otherwise.
363 // This function is horribly inefficient to implement with decNumber because
364 // the digits are stored least significant first, which requires moving all
365 // existing digits down one to make space for the new one to be appended.
368 DigitList::append(char digit
)
370 U_ASSERT(digit
>='0' && digit
<='9');
371 // Ignore digits which exceed the precision we can represent
372 // And don't fix for larger precision. Fix callers instead.
373 if (decNumberIsZero(fDecNumber
)) {
374 // Zero needs to be special cased because of the difference in the way
375 // that the old DigitList and decNumber represent it.
376 // digit cout was zero for digitList, is one for decNumber
377 fDecNumber
->lsu
[0] = digit
& 0x0f;
378 fDecNumber
->digits
= 1;
379 fDecNumber
->exponent
--; // To match the old digit list implementation.
381 int32_t nDigits
= fDecNumber
->digits
;
382 if (nDigits
< fContext
.digits
) {
384 for (i
=nDigits
; i
>0; i
--) {
385 fDecNumber
->lsu
[i
] = fDecNumber
->lsu
[i
-1];
387 fDecNumber
->lsu
[0] = digit
& 0x0f;
388 fDecNumber
->digits
++;
389 // DigitList emulation - appending doesn't change the magnitude of existing
390 // digits. With decNumber's decimal being after the
391 // least signficant digit, we need to adjust the exponent.
392 fDecNumber
->exponent
--;
398 char DigitList::getStrtodDecimalSeparator() {
399 // TODO: maybe use andy's pthread once.
400 static char gDecimal
= 0;
406 // We need to know the decimal separator character that will be used with strtod().
407 // Depends on the C runtime global locale.
408 // Most commonly is '.'
409 // TODO: caching could fail if the global locale is changed on the fly.
410 char rep
[MAX_DIGITS
];
411 sprintf(rep
, "%+1.1f", 1.0);
419 // -------------------------------------
422 * Currently, getDouble() depends on strtod() to do its conversion.
425 * This is an extremely costly function. ~1/2 of the conversion time
426 * can be linked to this function.
429 DigitList::getDouble() const
431 static char gDecimal
= 0;
432 char decimalSeparator
;
435 if (fHave
== kDouble
) {
436 return fUnion
.fDouble
;
438 decimalSeparator
= gDecimal
;
441 if (decimalSeparator
== 0) {
442 // We need to know the decimal separator character that will be used with strtod().
443 // Depends on the C runtime global locale.
444 // Most commonly is '.'
445 // TODO: caching could fail if the global locale is changed on the fly.
446 char rep
[MAX_DIGITS
];
447 sprintf(rep
, "%+1.1f", 1.0);
448 decimalSeparator
= rep
[2];
451 double tDouble
= 0.0;
454 if (decNumberIsNegative(fDecNumber
)) {
457 } else if (isInfinite()) {
458 if (std::numeric_limits
<double>::has_infinity
) {
459 tDouble
= std::numeric_limits
<double>::infinity();
461 tDouble
= std::numeric_limits
<double>::max();
464 tDouble
= -tDouble
; //this was incorrectly "-fDouble" originally.
467 MaybeStackArray
<char, MAX_DBL_DIGITS
+18> s
;
468 // Note: 14 is a magic constant from the decNumber library documentation,
469 // the max number of extra characters beyond the number of digits
470 // needed to represent the number in string form. Add a few more
471 // for the additional digits we retain.
473 // Round down to appx. double precision, if the number is longer than that.
474 // Copy the number first, so that we don't modify the original.
475 if (getCount() > MAX_DBL_DIGITS
+ 3) {
476 DigitList
numToConvert(*this);
477 numToConvert
.reduce(); // Removes any trailing zeros, so that digit count is good.
478 numToConvert
.round(MAX_DBL_DIGITS
+3);
479 uprv_decNumberToString(numToConvert
.fDecNumber
, s
.getAlias());
480 // TODO: how many extra digits should be included for an accurate conversion?
482 uprv_decNumberToString(this->fDecNumber
, s
.getAlias());
484 U_ASSERT(uprv_strlen(&s
[0]) < MAX_DBL_DIGITS
+18);
486 if (decimalSeparator
!= '.') {
487 char *decimalPt
= strchr(s
.getAlias(), '.');
488 if (decimalPt
!= NULL
) {
489 *decimalPt
= decimalSeparator
;
493 tDouble
= uprv_strtod(s
.getAlias(), &end
);
497 DigitList
*nonConstThis
= const_cast<DigitList
*>(this);
498 nonConstThis
->internalSetDouble(tDouble
);
499 gDecimal
= decimalSeparator
;
504 // -------------------------------------
507 * convert this number to an int32_t. Round if there is a fractional part.
508 * Return zero if the number cannot be represented.
510 int32_t DigitList::getLong() /*const*/
513 if (getUpperExponent() > 10) {
514 // Overflow, absolute value too big.
517 if (fDecNumber
->exponent
!= 0) {
518 // Force to an integer, with zero exponent, rounding if necessary.
519 // (decNumberToInt32 will only work if the exponent is exactly zero.)
520 DigitList
copy(*this);
522 uprv_decNumberQuantize(copy
.fDecNumber
, copy
.fDecNumber
, zero
.fDecNumber
, &fContext
);
523 result
= uprv_decNumberToInt32(copy
.fDecNumber
, &fContext
);
525 result
= uprv_decNumberToInt32(fDecNumber
, &fContext
);
532 * convert this number to an int64_t. Truncate if there is a fractional part.
533 * Return zero if the number cannot be represented.
535 int64_t DigitList::getInt64() /*const*/ {
536 // TODO: fast conversion if fHave == fDouble
538 // Truncate if non-integer.
539 // Return 0 if out of range.
540 // Range of in64_t is -9223372036854775808 to 9223372036854775807 (19 digits)
542 if (getUpperExponent() > 19) {
543 // Overflow, absolute value too big.
547 // The number of integer digits may differ from the number of digits stored
548 // in the decimal number.
549 // for 12.345 numIntDigits = 2, number->digits = 5
550 // for 12E4 numIntDigits = 6, number->digits = 2
551 // The conversion ignores the fraction digits in the first case,
552 // and fakes up extra zero digits in the second.
553 // TODO: It would be faster to store a table of powers of ten to multiply by
554 // instead of looping over zero digits, multiplying each time.
556 int32_t numIntDigits
= getUpperExponent();
558 for (int32_t i
= 0; i
< numIntDigits
; i
++) {
559 // Loop is iterating over digits starting with the most significant.
560 // Numbers are stored with the least significant digit at index zero.
561 int32_t digitIndex
= fDecNumber
->digits
- i
- 1;
562 int32_t v
= (digitIndex
>= 0) ? fDecNumber
->lsu
[digitIndex
] : 0;
563 value
= value
* (uint64_t)10 + (uint64_t)v
;
566 if (decNumberIsNegative(fDecNumber
)) {
570 int64_t svalue
= (int64_t)value
;
572 // Check overflow. It's convenient that the MSD is 9 only on overflow, the amount of
573 // overflow can't wrap too far. The test will also fail -0, but
574 // that does no harm; the right answer is 0.
575 if (numIntDigits
== 19) {
576 if (( decNumberIsNegative(fDecNumber
) && svalue
>0) ||
577 (!decNumberIsNegative(fDecNumber
) && svalue
<0)) {
587 * Return a string form of this number.
588 * Format is as defined by the decNumber library, for interchange of
591 void DigitList::getDecimal(CharString
&str
, UErrorCode
&status
) {
592 if (U_FAILURE(status
)) {
596 // A decimal number in string form can, worst case, be 14 characters longer
597 // than the number of digits. So says the decNumber library doc.
598 int32_t maxLength
= fDecNumber
->digits
+ 14;
599 int32_t capacity
= 0;
600 char *buffer
= str
.clear().getAppendBuffer(maxLength
, 0, capacity
, status
);
601 if (U_FAILURE(status
)) {
602 return; // Memory allocation error on growing the string.
604 U_ASSERT(capacity
>= maxLength
);
605 uprv_decNumberToString(this->fDecNumber
, buffer
);
606 U_ASSERT((int32_t)uprv_strlen(buffer
) <= maxLength
);
607 str
.append(buffer
, -1, status
);
611 * Return true if this is an integer value that can be held
612 * by an int32_t type.
615 DigitList::fitsIntoLong(UBool ignoreNegativeZero
) /*const*/
617 if (decNumberIsSpecial(this->fDecNumber
)) {
618 // NaN or Infinity. Does not fit in int32.
621 uprv_decNumberTrim(this->fDecNumber
);
622 if (fDecNumber
->exponent
< 0) {
623 // Number contains fraction digits.
626 if (decNumberIsZero(this->fDecNumber
) && !ignoreNegativeZero
&&
627 (fDecNumber
->bits
& DECNEG
) != 0) {
628 // Negative Zero, not ingored. Cannot represent as a long.
631 if (getUpperExponent() < 10) {
632 // The number is 9 or fewer digits.
633 // The max and min int32 are 10 digts, so this number fits.
634 // This is the common case.
638 // TODO: Should cache these constants; construction is relatively costly.
639 // But not of huge consequence; they're only needed for 10 digit ints.
640 UErrorCode status
= U_ZERO_ERROR
;
641 DigitList min32
; min32
.set("-2147483648", status
);
642 if (this->compare(min32
) < 0) {
645 DigitList max32
; max32
.set("2147483647", status
);
646 if (this->compare(max32
) > 0) {
649 if (U_FAILURE(status
)) {
658 * Return true if the number represented by this object can fit into
662 DigitList::fitsIntoInt64(UBool ignoreNegativeZero
) /*const*/
664 if (decNumberIsSpecial(this->fDecNumber
)) {
665 // NaN or Infinity. Does not fit in int32.
668 uprv_decNumberTrim(this->fDecNumber
);
669 if (fDecNumber
->exponent
< 0) {
670 // Number contains fraction digits.
673 if (decNumberIsZero(this->fDecNumber
) && !ignoreNegativeZero
&&
674 (fDecNumber
->bits
& DECNEG
) != 0) {
675 // Negative Zero, not ingored. Cannot represent as a long.
678 if (getUpperExponent() < 19) {
679 // The number is 18 or fewer digits.
680 // The max and min int64 are 19 digts, so this number fits.
681 // This is the common case.
685 // TODO: Should cache these constants; construction is relatively costly.
686 // But not of huge consequence; they're only needed for 19 digit ints.
687 UErrorCode status
= U_ZERO_ERROR
;
688 DigitList min64
; min64
.set("-9223372036854775808", status
);
689 if (this->compare(min64
) < 0) {
692 DigitList max64
; max64
.set("9223372036854775807", status
);
693 if (this->compare(max64
) > 0) {
696 if (U_FAILURE(status
)) {
703 // -------------------------------------
706 DigitList::set(int32_t source
)
708 set((int64_t)source
);
709 internalSetDouble(source
);
712 // -------------------------------------
714 * Set an int64, via decnumber
717 DigitList::set(int64_t source
)
719 char str
[MAX_DIGITS
+2]; // Leave room for sign and trailing nul.
720 formatBase10(source
, str
);
721 U_ASSERT(uprv_strlen(str
) < sizeof(str
));
723 uprv_decNumberFromString(fDecNumber
, str
, &fContext
);
724 internalSetDouble(static_cast<double>(source
));
727 // -------------------------------------
729 * Set the DigitList from a decimal number string.
731 * The incoming string _must_ be nul terminated, even though it is arriving
732 * as a StringPiece because that is what the decNumber library wants.
733 * We can get away with this for an internal function; it would not
734 * be acceptable for a public API.
737 DigitList::set(const StringPiece
&source
, UErrorCode
&status
, uint32_t /*fastpathBits*/) {
738 if (U_FAILURE(status
)) {
743 if(fastpathBits
==(kFastpathOk
|kNoDecimal
)) {
744 int32_t size
= source
.size();
745 const char *data
= source
.data();
750 char ch
= data
[--size
];
758 //printf("CH[%d]=%c, %d, *=%d\n", size,ch, (int)d, (int)m);
763 //printf("R=%d\n", r);
768 // Figure out a max number of digits to use during the conversion, and
769 // resize the number up if necessary.
770 int32_t numDigits
= source
.length();
771 if (numDigits
> fContext
.digits
) {
772 // fContext.digits == fStorage.getCapacity()
773 decNumber
*t
= fStorage
.resize(numDigits
, fStorage
.getCapacity());
775 status
= U_MEMORY_ALLOCATION_ERROR
;
779 fContext
.digits
= numDigits
;
783 uprv_decNumberFromString(fDecNumber
, source
.data(), &fContext
);
784 if ((fContext
.status
& DEC_Conversion_syntax
) != 0) {
785 status
= U_DECIMAL_NUMBER_SYNTAX_ERROR
;
792 * Set the digit list to a representation of the given double value.
793 * This method supports both fixed-point and exponential notation.
794 * @param source Value to be converted.
797 DigitList::set(double source
)
799 // for now, simple implementation; later, do proper IEEE stuff
800 char rep
[MAX_DIGITS
+ 8]; // Extra space for '+', '.', e+NNN, and '\0' (actually +8 is enough)
802 // Generate a representation of the form /[+-][0-9].[0-9]+e[+-][0-9]+/
803 // Can also generate /[+-]nan/ or /[+-]inf/
804 // TODO: Use something other than sprintf() here, since it's behavior is somewhat platform specific.
805 // That is why infinity is special cased here.
806 if (uprv_isInfinite(source
)) {
807 if (uprv_isNegativeInfinity(source
)) {
808 uprv_strcpy(rep
,"-inf"); // Handle negative infinity
810 uprv_strcpy(rep
,"inf");
813 sprintf(rep
, "%+1.*e", MAX_DBL_DIGITS
- 1, source
);
815 U_ASSERT(uprv_strlen(rep
) < sizeof(rep
));
817 // uprv_decNumberFromString() will parse the string expecting '.' as a
818 // decimal separator, however sprintf() can use ',' in certain locales.
819 // Overwrite a ',' with '.' here before proceeding.
820 char *decimalSeparator
= strchr(rep
, ',');
821 if (decimalSeparator
!= NULL
) {
822 *decimalSeparator
= '.';
825 // Create a decNumber from the string.
826 uprv_decNumberFromString(fDecNumber
, rep
, &fContext
);
827 uprv_decNumberTrim(fDecNumber
);
828 internalSetDouble(source
);
831 // -------------------------------------
835 * The number will be expanded if need be to retain full precision.
836 * In practice, for formatting, multiply is by 10, 100 or 1000, so more digits
837 * will not be required for this use.
840 DigitList::mult(const DigitList
&other
, UErrorCode
&status
) {
841 if (U_FAILURE(status
)) {
845 int32_t requiredDigits
= this->digits() + other
.digits();
846 if (requiredDigits
> fContext
.digits
) {
847 reduce(); // Remove any trailing zeros
848 int32_t requiredDigits
= this->digits() + other
.digits();
849 ensureCapacity(requiredDigits
, status
);
851 uprv_decNumberMultiply(fDecNumber
, fDecNumber
, other
.fDecNumber
, &fContext
);
855 // -------------------------------------
859 * The number will _not_ be expanded for inexact results.
860 * TODO: probably should expand some, for rounding increments that
861 * could add a few digits, e.g. .25, but not expand arbitrarily.
864 DigitList::div(const DigitList
&other
, UErrorCode
&status
) {
865 if (U_FAILURE(status
)) {
868 uprv_decNumberDivide(fDecNumber
, fDecNumber
, other
.fDecNumber
, &fContext
);
872 // -------------------------------------
875 * ensureCapacity. Grow the digit storage for the number if it's less than the requested
876 * amount. Never reduce it. Available size is kept in fContext.digits.
879 DigitList::ensureCapacity(int32_t requestedCapacity
, UErrorCode
&status
) {
880 if (U_FAILURE(status
)) {
883 if (requestedCapacity
<= 0) {
884 status
= U_ILLEGAL_ARGUMENT_ERROR
;
887 if (requestedCapacity
> DEC_MAX_DIGITS
) {
888 // Don't report an error for requesting too much.
889 // Arithemetic Results will be rounded to what can be supported.
890 // At 999,999,999 max digits, exceeding the limit is not too likely!
891 requestedCapacity
= DEC_MAX_DIGITS
;
893 if (requestedCapacity
> fContext
.digits
) {
894 decNumber
*newBuffer
= fStorage
.resize(requestedCapacity
, fStorage
.getCapacity());
895 if (newBuffer
== NULL
) {
896 status
= U_MEMORY_ALLOCATION_ERROR
;
899 fContext
.digits
= requestedCapacity
;
900 fDecNumber
= newBuffer
;
904 // -------------------------------------
907 * Round the representation to the given number of digits.
908 * @param maximumDigits The maximum number of digits to be shown.
909 * Upon return, count will be less than or equal to maximumDigits.
912 DigitList::round(int32_t maximumDigits
)
915 if (maximumDigits
>= fDecNumber
->digits
) {
918 int32_t savedDigits
= fContext
.digits
;
919 fContext
.digits
= maximumDigits
;
920 uprv_decNumberPlus(fDecNumber
, fDecNumber
, &fContext
);
921 fContext
.digits
= savedDigits
;
922 uprv_decNumberTrim(fDecNumber
);
929 DigitList::roundFixedPoint(int32_t maximumFractionDigits
) {
930 reduce(); // Remove trailing zeros.
931 if (fDecNumber
->exponent
>= -maximumFractionDigits
) {
934 decNumber scale
; // Dummy decimal number, but with the desired number of
935 uprv_decNumberZero(&scale
); // fraction digits.
936 scale
.exponent
= -maximumFractionDigits
;
939 uprv_decNumberQuantize(fDecNumber
, fDecNumber
, &scale
, &fContext
);
944 // -------------------------------------
947 DigitList::toIntegralValue() {
948 uprv_decNumberToIntegralValue(fDecNumber
, fDecNumber
, &fContext
);
952 // -------------------------------------
954 DigitList::isZero() const
956 return decNumberIsZero(fDecNumber
);
959 // -------------------------------------
961 DigitList::getUpperExponent() const {
962 return fDecNumber
->digits
+ fDecNumber
->exponent
;
966 DigitList::getSmallestInterval(DigitInterval
&result
) const {
967 result
.setLeastSignificantInclusive(fDecNumber
->exponent
);
968 result
.setMostSignificantExclusive(getUpperExponent());
973 DigitList::getDigitByExponent(int32_t exponent
) const {
974 int32_t idx
= exponent
- fDecNumber
->exponent
;
975 if (idx
< 0 || idx
>= fDecNumber
->digits
) {
978 return fDecNumber
->lsu
[idx
];
982 DigitList::appendDigitsTo(CharString
&str
, UErrorCode
&status
) const {
983 str
.append((const char *) fDecNumber
->lsu
, fDecNumber
->digits
, status
);
987 DigitList::roundAtExponent(int32_t exponent
, int32_t maxSigDigits
) {
989 if (maxSigDigits
< fDecNumber
->digits
) {
990 int32_t minExponent
= getUpperExponent() - maxSigDigits
;
991 if (exponent
< minExponent
) {
992 exponent
= minExponent
;
995 if (exponent
<= fDecNumber
->exponent
) {
998 int32_t digits
= getUpperExponent() - exponent
;
1002 roundFixedPoint(-exponent
);
1007 DigitList::quantize(const DigitList
&quantity
, UErrorCode
&status
) {
1008 if (U_FAILURE(status
)) {
1011 div(quantity
, status
);
1013 mult(quantity
, status
);
1018 DigitList::getScientificExponent(
1019 int32_t minIntDigitCount
, int32_t exponentMultiplier
) const {
1020 // The exponent for zero is always zero.
1024 int32_t intDigitCount
= getUpperExponent();
1026 if (intDigitCount
>= minIntDigitCount
) {
1027 int32_t maxAdjustment
= intDigitCount
- minIntDigitCount
;
1028 exponent
= (maxAdjustment
/ exponentMultiplier
) * exponentMultiplier
;
1030 int32_t minAdjustment
= minIntDigitCount
- intDigitCount
;
1031 exponent
= ((minAdjustment
+ exponentMultiplier
- 1) / exponentMultiplier
) * -exponentMultiplier
;
1037 DigitList::toScientific(
1038 int32_t minIntDigitCount
, int32_t exponentMultiplier
) {
1039 int32_t exponent
= getScientificExponent(
1040 minIntDigitCount
, exponentMultiplier
);
1041 shiftDecimalRight(-exponent
);
1046 DigitList::shiftDecimalRight(int32_t n
) {
1047 fDecNumber
->exponent
+= n
;
1052 #endif // #if !UCONFIG_NO_FORMATTING