2
*******************************************************************************
3
* Copyright (C) 1997-2001, International Business Machines Corporation and *
4
* others. All Rights Reserved. *
5
*******************************************************************************
9
* Modification History:
11
* Date Name Description
12
* 02/19/97 aliu Converted from java.
13
* 03/20/97 clhuang Implemented with new APIs.
14
* 03/31/97 aliu Moved isLONG_MIN to DigitList, and fixed it.
15
* 04/3/97 aliu Rewrote parsing and formatting completely, and
16
* cleaned up and debugged. Actually works now.
17
* Implemented NAN and INF handling, for both parsing
18
* and formatting. Extensive testing & debugging.
19
* 04/10/97 aliu Modified to compile on AIX.
20
* 04/16/97 aliu Rewrote to use DigitList, which has been resurrected.
21
* Changed DigitCount to int per code review.
22
* 07/09/97 helena Made ParsePosition into a class.
23
* 08/26/97 aliu Extensive changes to applyPattern; completely
24
* rewritten from the Java.
25
* 09/09/97 aliu Ported over support for exponential formats.
26
* 07/20/98 stephen JDK 1.2 sync up.
27
* Various instances of '0' replaced with 'NULL'
28
* Check for grouping size in subFormat()
29
* Brought subParse() in line with Java 1.2
30
* Added method appendAffix()
31
* 08/24/1998 srl Removed Mutex calls. This is not a thread safe class!
32
* 02/22/99 stephen Removed character literals for EBCDIC safety
33
* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
34
* 06/28/99 stephen Fixed bugs in toPattern().
35
* 06/29/99 stephen Fixed operator= to copy fFormatWidth, fPad,
37
********************************************************************************
40
#include "unicode/decimfmt.h"
42
#include "unicode/dcfmtsym.h"
43
#include "unicode/resbund.h"
44
#include "unicode/uchar.h"
53
static void debugout(UnicodeString s) {
55
s.extract((int32_t) 0, s.length(), buf);
58
#define debug(x) printf("%s", x);
64
// *****************************************************************************
65
// class DecimalFormat
66
// *****************************************************************************
68
const char DecimalFormat::fgClassID = 0; // Value is irrelevant
70
// Constants for characters used in programmatic (unlocalized) patterns.
71
const UChar DecimalFormat::kPatternZeroDigit = 0x0030 /*'0'*/;
72
const UChar DecimalFormat::kPatternGroupingSeparator = 0x002C /*','*/;
73
const UChar DecimalFormat::kPatternDecimalSeparator = 0x002E /*'.'*/;
74
const UChar DecimalFormat::kPatternPerMill = 0x2030;
75
const UChar DecimalFormat::kPatternPercent = 0x0025 /*'%'*/;
76
const UChar DecimalFormat::kPatternDigit = 0x0023 /*'#'*/;
77
const UChar DecimalFormat::kPatternSeparator = 0x003B /*';'*/;
78
const UChar DecimalFormat::kPatternExponent = 0x0045 /*'E'*/;
79
const UChar DecimalFormat::kPatternPlus = 0x002B /*'+'*/;
80
const UChar DecimalFormat::kPatternMinus = 0x002D /*'-'*/;
81
const UChar DecimalFormat::kPatternPadEscape = 0x002A /*'*'*/;
82
const UChar DecimalFormat::kCurrencySign = 0x00A4;
83
const UChar DecimalFormat::kQuote = 0x0027 /*'\''*/;
85
//const int8_t DecimalFormat::fgMaxDigit = 9;
87
const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
88
const int32_t DecimalFormat::kDoubleFractionDigits = 340;
91
* These are the tags we expect to see in normal resource bundle files associated
94
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns";
96
//------------------------------------------------------------------------------
97
// Constructs a DecimalFormat instance in the default locale.
99
DecimalFormat::DecimalFormat(UErrorCode& status)
101
fPosPrefixPattern(0),
102
fPosSuffixPattern(0),
103
fNegPrefixPattern(0),
104
fNegSuffixPattern(0),
107
UParseError parseError;
108
construct(status, parseError);
111
//------------------------------------------------------------------------------
112
// Constructs a DecimalFormat instance with the specified number format
113
// pattern in the default locale.
115
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
118
fPosPrefixPattern(0),
119
fPosSuffixPattern(0),
120
fNegPrefixPattern(0),
121
fNegSuffixPattern(0),
124
UParseError parseError;
125
construct(status, parseError, &pattern);
128
//------------------------------------------------------------------------------
129
// Constructs a DecimalFormat instance with the specified number format
130
// pattern and the number format symbols in the default locale. The
131
// created instance owns the symbols.
133
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
134
DecimalFormatSymbols* symbolsToAdopt,
137
fPosPrefixPattern(0),
138
fPosSuffixPattern(0),
139
fNegPrefixPattern(0),
140
fNegSuffixPattern(0),
143
UParseError parseError;
144
if (symbolsToAdopt == NULL)
145
status = U_ILLEGAL_ARGUMENT_ERROR;
146
construct(status, parseError, &pattern, symbolsToAdopt);
149
DecimalFormat::DecimalFormat( const UnicodeString& pattern,
150
DecimalFormatSymbols* symbolsToAdopt,
151
UParseError& parseErr,
154
fPosPrefixPattern(0),
155
fPosSuffixPattern(0),
156
fNegPrefixPattern(0),
157
fNegSuffixPattern(0),
160
if (symbolsToAdopt == NULL)
161
status = U_ILLEGAL_ARGUMENT_ERROR;
162
construct(status,parseErr, &pattern, symbolsToAdopt);
164
//------------------------------------------------------------------------------
165
// Constructs a DecimalFormat instance with the specified number format
166
// pattern and the number format symbols in the default locale. The
167
// created instance owns the clone of the symbols.
169
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
170
const DecimalFormatSymbols& symbols,
173
fPosPrefixPattern(0),
174
fPosSuffixPattern(0),
175
fNegPrefixPattern(0),
176
fNegSuffixPattern(0),
179
UParseError parseError;
180
construct(status, parseError, &pattern, new DecimalFormatSymbols(symbols));
183
//------------------------------------------------------------------------------
184
// Constructs a DecimalFormat instance with the specified number format
185
// pattern and the number format symbols in the desired locale. The
186
// created instance owns the symbols.
189
DecimalFormat::construct(UErrorCode& status,
190
UParseError& parseErr,
191
const UnicodeString* pattern,
192
DecimalFormatSymbols* symbolsToAdopt,
193
const Locale& locale)
195
fSymbols = symbolsToAdopt; // Do this BEFORE aborting on status failure!!!
196
// fDigitList = new DigitList(); // Do this BEFORE aborting on status failure!!!
197
fRoundingIncrement = NULL;
198
fRoundingDouble = 0.0;
199
fRoundingMode = kRoundHalfEven;
200
fPad = kPatternPadEscape;
201
fPadPosition = kPadBeforePrefix;
202
if (U_FAILURE(status))
205
fPosPrefixPattern = fPosSuffixPattern = NULL;
206
fNegPrefixPattern = fNegSuffixPattern = NULL;
210
fDecimalSeparatorAlwaysShown = FALSE;
211
fIsCurrencyFormat = FALSE;
212
fUseExponentialNotation = FALSE;
213
fMinExponentDigits = 0;
215
if (fSymbols == NULL)
217
fSymbols = new DecimalFormatSymbols(locale, status);
221
// Uses the default locale's number format pattern if there isn't
225
ResourceBundle resource((char *)0, Locale::getDefault(), status);
227
str = resource.get(fgNumberPatterns, status).getStringEx((int32_t)0, status);
231
if (U_FAILURE(status))
236
applyPattern(*pattern, FALSE /*not localized*/,parseErr, status);
239
//------------------------------------------------------------------------------
241
DecimalFormat::~DecimalFormat()
243
// delete fDigitList;
244
delete fPosPrefixPattern;
245
delete fPosSuffixPattern;
246
delete fNegPrefixPattern;
247
delete fNegSuffixPattern;
249
delete fRoundingIncrement;
252
//------------------------------------------------------------------------------
255
DecimalFormat::DecimalFormat(const DecimalFormat &source)
256
: NumberFormat(source),
258
fPosPrefixPattern(NULL),
259
fPosSuffixPattern(NULL),
260
fNegPrefixPattern(NULL),
261
fNegSuffixPattern(NULL),
263
fRoundingIncrement(NULL)
268
//------------------------------------------------------------------------------
269
// assignment operator
270
// Note that fDigitList is not considered a significant part of the
271
// DecimalFormat because it's used as a buffer to process the numbers.
273
static void _copy_us_ptr(UnicodeString** pdest, const UnicodeString* source) {
274
if (source == NULL) {
277
} else if (*pdest == NULL) {
278
*pdest = new UnicodeString(*source);
285
DecimalFormat::operator=(const DecimalFormat& rhs)
288
NumberFormat::operator=(rhs);
289
fPositivePrefix = rhs.fPositivePrefix;
290
fPositiveSuffix = rhs.fPositiveSuffix;
291
fNegativePrefix = rhs.fNegativePrefix;
292
fNegativeSuffix = rhs.fNegativeSuffix;
293
_copy_us_ptr(&fPosPrefixPattern, rhs.fPosPrefixPattern);
294
_copy_us_ptr(&fPosSuffixPattern, rhs.fPosSuffixPattern);
295
_copy_us_ptr(&fNegPrefixPattern, rhs.fNegPrefixPattern);
296
_copy_us_ptr(&fNegSuffixPattern, rhs.fNegSuffixPattern);
297
if(rhs.fRoundingIncrement == NULL) {
298
delete fRoundingIncrement;
299
fRoundingIncrement = NULL;
301
else if(fRoundingIncrement == NULL) {
302
fRoundingIncrement = new DigitList(*rhs.fRoundingIncrement);
305
*fRoundingIncrement = *rhs.fRoundingIncrement;
307
fRoundingDouble = rhs.fRoundingDouble;
308
fMultiplier = rhs.fMultiplier;
309
fGroupingSize = rhs.fGroupingSize;
310
fGroupingSize2 = rhs.fGroupingSize2;
311
fDecimalSeparatorAlwaysShown = rhs.fDecimalSeparatorAlwaysShown;
313
fSymbols = new DecimalFormatSymbols(*rhs.fSymbols);
315
*fSymbols = *rhs.fSymbols;
316
fUseExponentialNotation = rhs.fUseExponentialNotation;
317
fExponentSignAlwaysShown = rhs.fExponentSignAlwaysShown;
318
/*Bertrand A. D. Update 98.03.17*/
319
fIsCurrencyFormat = rhs.fIsCurrencyFormat;
321
fMinExponentDigits = rhs.fMinExponentDigits;
322
// if (fDigitList == NULL)
323
// fDigitList = new DigitList();
326
fFormatWidth = rhs.fFormatWidth;
328
fPadPosition = rhs.fPadPosition;
334
//------------------------------------------------------------------------------
337
DecimalFormat::operator==(const Format& that) const
342
if (getDynamicClassID() != that.getDynamicClassID())
345
const DecimalFormat* other = (DecimalFormat*)&that;
348
// This code makes it easy to determine why two format objects that should
351
if (!NumberFormat::operator==(that)) {
352
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
353
debug("NumberFormat::!=");
355
if (!((fPosPrefixPattern == other->fPosPrefixPattern && // both null
356
fPositivePrefix == other->fPositivePrefix)
357
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
358
*fPosPrefixPattern == *other->fPosPrefixPattern))) {
359
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
360
debug("Pos Prefix !=");
362
if (!((fPosSuffixPattern == other->fPosSuffixPattern && // both null
363
fPositiveSuffix == other->fPositiveSuffix)
364
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
365
*fPosSuffixPattern == *other->fPosSuffixPattern))) {
366
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
367
debug("Pos Suffix !=");
369
if (!((fNegPrefixPattern == other->fNegPrefixPattern && // both null
370
fNegativePrefix == other->fNegativePrefix)
371
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
372
*fNegPrefixPattern == *other->fNegPrefixPattern))) {
373
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
374
debug("Neg Prefix ");
375
if (fNegPrefixPattern == NULL) {
377
debugout(fNegativePrefix);
380
debugout(*fNegPrefixPattern);
383
if (other->fNegPrefixPattern == NULL) {
385
debugout(other->fNegativePrefix);
388
debugout(*other->fNegPrefixPattern);
391
if (!((fNegSuffixPattern == other->fNegSuffixPattern && // both null
392
fNegativeSuffix == other->fNegativeSuffix)
393
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
394
*fNegSuffixPattern == *other->fNegSuffixPattern))) {
395
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
396
debug("Neg Suffix ");
397
if (fNegSuffixPattern == NULL) {
399
debugout(fNegativeSuffix);
402
debugout(*fNegSuffixPattern);
405
if (other->fNegSuffixPattern == NULL) {
407
debugout(other->fNegativeSuffix);
410
debugout(*other->fNegSuffixPattern);
413
if (!((fRoundingIncrement == other->fRoundingIncrement) // both null
414
|| (fRoundingIncrement != NULL &&
415
other->fRoundingIncrement != NULL &&
416
*fRoundingIncrement == *other->fRoundingIncrement))) {
417
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
418
debug("Rounding Increment !=");
420
if (fMultiplier != other->fMultiplier) {
421
if (first) { printf("[ "); first = FALSE; }
422
printf("Multiplier %ld != %ld", fMultiplier, other->fMultiplier);
424
if (fGroupingSize != other->fGroupingSize) {
425
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
426
printf("Grouping Size %ld != %ld", fGroupingSize, other->fGroupingSize);
428
if (fGroupingSize2 != other->fGroupingSize2) {
429
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
430
printf("Secondary Grouping Size %ld != %ld", fGroupingSize2, other->fGroupingSize2);
432
if (fDecimalSeparatorAlwaysShown != other->fDecimalSeparatorAlwaysShown) {
433
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
434
printf("Dec Sep Always %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
436
if (fUseExponentialNotation != other->fUseExponentialNotation) {
437
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
440
if (!(!fUseExponentialNotation ||
441
fMinExponentDigits != other->fMinExponentDigits)) {
442
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
443
debug("Exp Digits !=");
445
if (*fSymbols != *(other->fSymbols)) {
446
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
449
if (!first) { printf(" ]"); }
452
return (NumberFormat::operator==(that) &&
453
((fPosPrefixPattern == other->fPosPrefixPattern && // both null
454
fPositivePrefix == other->fPositivePrefix)
455
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
456
*fPosPrefixPattern == *other->fPosPrefixPattern)) &&
457
((fPosSuffixPattern == other->fPosSuffixPattern && // both null
458
fPositiveSuffix == other->fPositiveSuffix)
459
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
460
*fPosSuffixPattern == *other->fPosSuffixPattern)) &&
461
((fNegPrefixPattern == other->fNegPrefixPattern && // both null
462
fNegativePrefix == other->fNegativePrefix)
463
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
464
*fNegPrefixPattern == *other->fNegPrefixPattern)) &&
465
((fNegSuffixPattern == other->fNegSuffixPattern && // both null
466
fNegativeSuffix == other->fNegativeSuffix)
467
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
468
*fNegSuffixPattern == *other->fNegSuffixPattern)) &&
469
((fRoundingIncrement == other->fRoundingIncrement) // both null
470
|| (fRoundingIncrement != NULL &&
471
other->fRoundingIncrement != NULL &&
472
*fRoundingIncrement == *other->fRoundingIncrement)) &&
473
fMultiplier == other->fMultiplier &&
474
fGroupingSize == other->fGroupingSize &&
475
fGroupingSize2 == other->fGroupingSize2 &&
476
fDecimalSeparatorAlwaysShown == other->fDecimalSeparatorAlwaysShown &&
477
fUseExponentialNotation == other->fUseExponentialNotation &&
478
(!fUseExponentialNotation ||
479
fMinExponentDigits == other->fMinExponentDigits) &&
480
*fSymbols == *(other->fSymbols));
483
//------------------------------------------------------------------------------
486
DecimalFormat::clone() const
488
return new DecimalFormat(*this);
491
//------------------------------------------------------------------------------
494
DecimalFormat::format(int32_t number,
495
UnicodeString& result,
496
FieldPosition& fieldPosition) const
500
// Clears field positions.
501
fieldPosition.setBeginIndex(0);
502
fieldPosition.setEndIndex(0);
504
// If we are to do rounding, we need to move into the BigDecimal
505
// domain in order to do divide/multiply correctly.
507
// In general, long values always represent real finite numbers, so
508
// we don't have to check for +/- Infinity or NaN. However, there
509
// is one case we have to be careful of: The multiplier can push
510
// a number near MIN_VALUE or MAX_VALUE outside the legal range. We
511
// check for this before multiplying, and if it happens we use doubles
512
// instead, trading off accuracy for range.
513
if (fRoundingIncrement != NULL
514
|| (fMultiplier != 0 && (number > (INT32_MAX / fMultiplier)
515
|| number < (INT32_MIN / fMultiplier))))
517
digits.set(((double)number) * fMultiplier,
518
fUseExponentialNotation ?
519
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0,
520
!fUseExponentialNotation);
524
digits.set(number * fMultiplier,
525
fUseExponentialNotation ?
526
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0);
529
return subformat(result, fieldPosition, digits, TRUE);
532
//------------------------------------------------------------------------------
535
DecimalFormat::format( double number,
536
UnicodeString& result,
537
FieldPosition& fieldPosition) const
539
// Clears field positions.
540
fieldPosition.setBeginIndex(0);
541
fieldPosition.setEndIndex(0);
543
// Special case for NaN, sets the begin and end index to be the
544
// the string length of localized name of NaN.
545
if (uprv_isNaN(number))
547
if (fieldPosition.getField() == NumberFormat::kIntegerField)
548
fieldPosition.setBeginIndex(result.length());
550
result += fSymbols->getSymbol(DecimalFormatSymbols::kNaNSymbol);
552
if (fieldPosition.getField() == NumberFormat::kIntegerField)
553
fieldPosition.setEndIndex(result.length());
555
addPadding(result, fieldPosition, FALSE, FALSE /*ignored*/);
559
/* Detecting whether a double is negative is easy with the exception of
560
* the value -0.0. This is a double which has a zero mantissa (and
561
* exponent), but a negative sign bit. It is semantically distinct from
562
* a zero with a positive sign bit, and this distinction is important
563
* to certain kinds of computations. However, it's a little tricky to
564
* detect, since (-0.0 == 0.0) and !(-0.0 < 0.0). How then, you may
565
* ask, does it behave distinctly from +0.0? Well, 1/(-0.0) ==
566
* -Infinity. Proper detection of -0.0 is needed to deal with the
567
* issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
569
UBool isNegative = uprv_isNegative(number);
571
// Do this BEFORE checking to see if value is infinite! Sets the
572
// begin and end index to be length of the string composed of
573
// localized name of Infinite and the positive/negative localized
576
number *= fMultiplier;
578
// Apply rounding after multiplier
579
if (fRoundingIncrement != NULL) {
580
if (isNegative) // For rounding in the correct direction
582
number = fRoundingDouble
583
* round(number / fRoundingDouble, fRoundingMode, isNegative);
588
// Special case for INFINITE,
589
if (uprv_isInfinite(number))
591
result += (isNegative ? fNegativePrefix : fPositivePrefix);
593
if (fieldPosition.getField() == NumberFormat::kIntegerField)
594
fieldPosition.setBeginIndex(result.length());
596
result += fSymbols->getSymbol(DecimalFormatSymbols::kInfinitySymbol);
598
if (fieldPosition.getField() == NumberFormat::kIntegerField)
599
fieldPosition.setEndIndex(result.length());
601
result += (isNegative ? fNegativeSuffix : fPositiveSuffix);
603
addPadding(result, fieldPosition, TRUE, isNegative);
609
// This detects negativity too.
610
digits.set(number, fUseExponentialNotation ?
611
getMinimumIntegerDigits() + getMaximumFractionDigits() :
612
getMaximumFractionDigits(),
613
!fUseExponentialNotation);
615
return subformat(result, fieldPosition, digits, FALSE);
619
* Round a double value to the nearest integer according to the
621
* @param a the absolute value of the number to be rounded
622
* @param mode a BigDecimal rounding mode
623
* @param isNegative true if the number to be rounded is negative
624
* @return the absolute value of the rounded result
626
double DecimalFormat::round(double a, ERoundingMode mode, UBool isNegative) {
629
return isNegative ? uprv_floor(a) : uprv_ceil(a);
631
return isNegative ? uprv_ceil(a) : uprv_floor(a);
633
return uprv_floor(a);
638
double f = uprv_floor(a);
639
if ((a - f) != 0.5) {
640
return uprv_floor(a + 0.5);
643
return (g == uprv_floor(g)) ? f : (f + 1.0);
646
return ((a - uprv_floor(a)) <= 0.5) ? uprv_floor(a) : uprv_ceil(a);
648
return ((a - uprv_floor(a)) < 0.5) ? uprv_floor(a) : uprv_ceil(a);
654
DecimalFormat::format( const Formattable& obj,
655
UnicodeString& result,
656
FieldPosition& fieldPosition,
657
UErrorCode& status) const
659
return NumberFormat::format(obj, result, fieldPosition, status);
663
* Return true if a grouping separator belongs at the given
664
* position, based on whether grouping is in use and the values of
665
* the primary and secondary grouping interval.
666
* @param pos the number of integer digits to the right of
667
* the current position. Zero indicates the position after the
668
* rightmost integer digit.
669
* @return true if a grouping character belongs at the current
672
UBool DecimalFormat::isGroupingPosition(int32_t pos) const {
673
UBool result = FALSE;
674
if (isGroupingUsed() && (pos > 0) && (fGroupingSize > 0)) {
675
if ((fGroupingSize2 > 0) && (pos > fGroupingSize)) {
676
result = ((pos - fGroupingSize) % fGroupingSize2) == 0;
678
result = pos % fGroupingSize == 0;
684
//------------------------------------------------------------------------------
687
* Complete the formatting of a finite number. On entry, the fDigitList must
688
* be filled in with the correct digits.
691
DecimalFormat::subformat(UnicodeString& result,
692
FieldPosition& fieldPosition,
694
UBool isInteger) const
696
// Gets the localized zero Unicode character.
697
UChar32 zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
698
int32_t zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
699
UnicodeString grouping(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
700
UnicodeString decimal(fIsCurrencyFormat ?
701
fSymbols->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol) :
702
fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
703
int32_t maxIntDig = getMaximumIntegerDigits();
704
int32_t minIntDig = getMinimumIntegerDigits();
706
/* Per bug 4147706, DecimalFormat must respect the sign of numbers which
707
* format as zero. This allows sensible computations and preserves
708
* relations such as signum(1/x) = signum(x), where x is +Infinity or
709
* -Infinity. Prior to this fix, we always formatted zero values as if
710
* they were positive. Liu 7/6/98.
714
digits.fDecimalAt = digits.fCount = 0; // Normalize
717
// Appends the prefix.
718
result += (digits.fIsPositive ? fPositivePrefix : fNegativePrefix);
720
if (fUseExponentialNotation)
722
// Record field information for caller.
723
if (fieldPosition.getField() == NumberFormat::kIntegerField)
725
fieldPosition.setBeginIndex(result.length());
726
fieldPosition.setEndIndex(-1);
728
else if (fieldPosition.getField() == NumberFormat::kFractionField)
730
fieldPosition.setBeginIndex(-1);
733
// Minimum integer digits are handled in exponential format by
734
// adjusting the exponent. For example, 0.01234 with 3 minimum
735
// integer digits is "123.4E-4".
737
// Maximum integer digits are interpreted as indicating the
738
// repeating range. This is useful for engineering notation, in
739
// which the exponent is restricted to a multiple of 3. For
740
// example, 0.01234 with 3 maximum integer digits is "12.34e-3".
741
// If maximum integer digits are defined and are larger than
742
// minimum integer digits, then minimum integer digits are
744
int32_t exponent = digits.fDecimalAt;
745
if (maxIntDig > 1 && maxIntDig != minIntDig) {
746
// A exponent increment is defined; adjust to it.
747
exponent = (exponent > 0) ? (exponent - 1) / maxIntDig
748
: (exponent / maxIntDig) - 1;
749
exponent *= maxIntDig;
751
// No exponent increment is defined; use minimum integer digits.
752
// If none is specified, as in "#E0", generate 1 integer digit.
753
exponent -= (minIntDig > 0 || getMinimumFractionDigits() > 0)
757
// We now output a minimum number of digits, and more if there
758
// are more digits, up to the maximum number of digits. We
759
// place the decimal point after the "integer" digits, which
760
// are the first (decimalAt - exponent) digits.
761
int32_t minimumDigits = minIntDig + getMinimumFractionDigits();
762
// The number of integer digits is handled specially if the number
763
// is zero, since then there may be no digits.
764
int32_t integerDigits = digits.isZero() ? minIntDig :
765
digits.fDecimalAt - exponent;
766
int32_t totalDigits = digits.fCount;
767
if (minimumDigits > totalDigits)
768
totalDigits = minimumDigits;
769
if (integerDigits > totalDigits)
770
totalDigits = integerDigits;
772
// totalDigits records total number of digits needs to be processed
774
for (i=0; i<totalDigits; ++i)
776
if (i == integerDigits)
778
// Record field information for caller.
779
if (fieldPosition.getField() == NumberFormat::kIntegerField)
780
fieldPosition.setEndIndex(result.length());
784
// Record field information for caller.
785
if (fieldPosition.getField() == NumberFormat::kFractionField)
786
fieldPosition.setBeginIndex(result.length());
788
// Restores the digit character or pads the buffer with zeros.
789
UChar32 c = (UChar32)((i < digits.fCount) ?
790
(digits.fDigits[i] + zeroDelta) :
795
// Record field information
796
if (fieldPosition.getField() == NumberFormat::kIntegerField)
798
if (fieldPosition.getEndIndex() < 0)
799
fieldPosition.setEndIndex(result.length());
801
else if (fieldPosition.getField() == NumberFormat::kFractionField)
803
if (fieldPosition.getBeginIndex() < 0)
804
fieldPosition.setBeginIndex(result.length());
805
fieldPosition.setEndIndex(result.length());
808
// The exponent is output using the pattern-specified minimum
809
// exponent digits. There is no maximum limit to the exponent
810
// digits, since truncating the exponent would result in an
811
// unacceptable inaccuracy.
812
result += fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol);
814
// For zero values, we force the exponent to zero. We
815
// must do this here, and not earlier, because the value
816
// is used to determine integer digit count above.
821
result += fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol);
822
} else if (fExponentSignAlwaysShown) {
823
result += fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol);
827
expDigits.set(exponent);
828
for (i=expDigits.fDecimalAt; i<fMinExponentDigits; ++i)
830
for (i=0; i<expDigits.fDecimalAt; ++i)
832
UChar32 c = (UChar32)((i < expDigits.fCount) ?
833
(expDigits.fDigits[i] + zeroDelta) : zero);
837
else // Not using exponential notation
839
// Record field information for caller.
840
if (fieldPosition.getField() == NumberFormat::kIntegerField)
841
fieldPosition.setBeginIndex(result.length());
843
// Output the integer portion. Here 'count' is the total
844
// number of integer digits we will display, including both
845
// leading zeros required to satisfy getMinimumIntegerDigits,
846
// and actual digits present in the number.
847
int32_t count = minIntDig;
848
int32_t digitIndex = 0; // Index into digits.fDigits[]
849
if (digits.fDecimalAt > 0 && count < digits.fDecimalAt)
850
count = digits.fDecimalAt;
852
// Handle the case where getMaximumIntegerDigits() is smaller
853
// than the real number of integer digits. If this is so, we
854
// output the least significant max integer digits. For example,
855
// the value 1997 printed with 2 max integer digits is just "97".
857
if (count > maxIntDig)
860
digitIndex = digits.fDecimalAt - count;
863
int32_t sizeBeforeIntegerPart = result.length();
866
for (i=count-1; i>=0; --i)
868
if (i < digits.fDecimalAt && digitIndex < digits.fCount)
870
// Output a real digit
871
result += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
875
// Output a leading zero
879
// Output grouping separator if necessary.
880
if (isGroupingPosition(i)) {
881
result.append(grouping);
885
// Record field information for caller.
886
if (fieldPosition.getField() == NumberFormat::kIntegerField)
887
fieldPosition.setEndIndex(result.length());
889
// Determine whether or not there are any printable fractional
890
// digits. If we've used up the digits we know there aren't.
891
UBool fractionPresent = (getMinimumFractionDigits() > 0) ||
892
(!isInteger && digitIndex < digits.fCount);
894
// If there is no fraction present, and we haven't printed any
895
// integer digits, then print a zero. Otherwise we won't print
896
// _any_ digits, and we won't be able to parse this string.
897
if (!fractionPresent && result.length() == sizeBeforeIntegerPart)
900
// Output the decimal separator if we always do so.
901
if (fDecimalSeparatorAlwaysShown || fractionPresent)
904
// Record field information for caller.
905
if (fieldPosition.getField() == NumberFormat::kFractionField)
906
fieldPosition.setBeginIndex(result.length());
908
int32_t maxFracDigits = getMaximumFractionDigits();
909
int32_t negDecimalAt = -digits.fDecimalAt;
910
for (i=0; i < maxFracDigits; ++i)
912
if (!isInteger && digitIndex < digits.fCount)
914
if (i >= negDecimalAt)
917
result += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
921
// Output leading fractional zeros. These are zeros that come after
922
// the decimal but before any significant digits. These are only
923
// output if abs(number being formatted) < 1.0.
929
// Here is where we escape from the loop. We escape if we've output
930
// the maximum fraction digits (specified in the for expression above).
931
// We also stop when we've output the minimum digits and either:
932
// we have an integer, so there is no fractional stuff to display,
933
// or we're out of significant digits.
934
if (i >= getMinimumFractionDigits())
937
// No precision is left.
942
// Record field information for caller.
943
if (fieldPosition.getField() == NumberFormat::kFractionField)
944
fieldPosition.setEndIndex(result.length());
947
result += (digits.fIsPositive ? fPositiveSuffix : fNegativeSuffix);
949
addPadding(result, fieldPosition, TRUE, !digits.fIsPositive);
954
* Inserts the character fPad as needed to expand result to fFormatWidth.
955
* @param result the string to be padded
956
* @param hasAffixes if true, padding is positioned appropriately before or
957
* after affixes. If false, then isNegative is ignored, and there are only
958
* two effective pad positions: kPadBeforePrefix/kPadAfterPrefix and
959
* kPadBeforeSuffix/kPadAfterSuffix.
960
* @param isNegative must be true if result contains a formatted negative
961
* number, and false otherwise. Ignored if hasAffixes is false.
963
void DecimalFormat::addPadding(UnicodeString& result,
964
FieldPosition& fieldPosition,
966
UBool isNegative) const
968
if (fFormatWidth > 0) {
969
int32_t len = fFormatWidth - result.length();
971
UnicodeString padding;
972
for (int32_t i=0; i<len; ++i) {
975
switch (fPadPosition) {
976
case kPadAfterPrefix:
978
result.insert(isNegative ? fNegativePrefix.length()
979
: fPositivePrefix.length(),
982
} // else fall through to next case
983
case kPadBeforePrefix:
984
result.insert(0, padding);
986
case kPadBeforeSuffix:
988
result.insert(result.length() -
989
(isNegative ? fNegativeSuffix.length()
990
: fPositiveSuffix.length()),
993
} // else fall through to next case
994
case kPadAfterSuffix:
998
fieldPosition.setBeginIndex(len + fieldPosition.getBeginIndex());
999
fieldPosition.setEndIndex(len + fieldPosition.getEndIndex());
1004
//------------------------------------------------------------------------------
1007
DecimalFormat::parse(const UnicodeString& text,
1008
Formattable& result,
1009
UErrorCode& status) const
1011
NumberFormat::parse(text, result, status);
1015
DecimalFormat::parse(const UnicodeString& text,
1016
Formattable& result,
1017
ParsePosition& parsePosition) const
1019
int32_t backup = parsePosition.getIndex();
1021
int32_t padLen = fPad.length();
1023
// Skip padding characters, if any
1024
if (fFormatWidth > 0) {
1025
i = parsePosition.getIndex();
1026
while (i < text.length() && !text.compare(i, padLen, fPad, 0, padLen)) {
1029
parsePosition.setIndex(i);
1033
// If the text is composed of the representation of NaN, returns NaN.length
1034
UnicodeString nan(fSymbols->getSymbol(DecimalFormatSymbols::kNaNSymbol));
1035
int32_t nanLen = (text.compare(parsePosition.getIndex(), nan.length(), nan)
1036
? 0 : nan.length());
1038
parsePosition.setIndex(parsePosition.getIndex() + nanLen);
1039
result.setDouble(uprv_getNaN());
1043
// status is used to record whether a number is infinite.
1044
UBool status[fgStatusLength];
1047
if (!subparse(text, parsePosition, digits, status)) {
1048
parsePosition.setIndex(backup);
1051
if (fFormatWidth < 0) {
1052
i = parsePosition.getIndex();
1053
while (i < text.length() && !text.compare(i, padLen, fPad, 0, padLen)) {
1056
parsePosition.setIndex(i);
1060
if (status[fgStatusInfinite]) {
1061
double inf = uprv_getInfinity();
1062
result.setDouble(digits.fIsPositive ? inf : -inf);
1066
// Do as much of the multiplier conversion as possible without
1068
int32_t mult = fMultiplier; // Don't modify this.multiplier
1069
while (mult % 10 == 0) {
1071
--digits.fDecimalAt;
1074
// Handle integral values. We want to return the most
1075
// parsimonious type that will accommodate all of the result's
1076
// precision. We therefore only return a long if the result fits
1077
// entirely within a long (taking into account the multiplier) --
1078
// otherwise we fall through and return a double. When more
1079
// numeric types are supported by Formattable (e.g., 64-bit
1080
// integers, bignums) we will extend this logic to include them.
1081
if (digits.fitsIntoLong(isParseIntegerOnly())) {
1082
int32_t n = digits.getLong();
1083
if (n % mult == 0) {
1084
result.setLong(n / mult);
1087
else { // else handle the remainder
1088
result.setDouble(((double)n) / mult);
1093
// Handle non-integral or very large values
1094
// Dividing by one is okay and not that costly.
1095
result.setDouble(digits.getDouble() / mult);
1102
This is an old implimentation that was preparing for 64-bit numbers in ICU.
1103
It is very slow, and 64-bit numbers are not ANSI-C compatible. This code
1104
is here if we change our minds.
1107
* Parse the given text into a number. The text is parsed beginning at
1108
* parsePosition, until an unparseable character is seen.
1109
* @param text The string to parse.
1110
* @param parsePosition The position at which to being parsing. Upon
1111
* return, the first unparseable character.
1112
* @param digits The DigitList to set to the parsed value.
1113
* @param isExponent If true, parse an exponent. This means no
1114
* infinite values and integer only. By default it's really false.
1115
* @param status Upon return contains boolean status flags indicating
1116
* whether the value was infinite and whether it was positive.
1118
UBool DecimalFormat::subparse(const UnicodeString& text, ParsePosition& parsePosition,
1119
DigitList& digits, UBool* status) const
1121
int32_t position = parsePosition.getIndex();
1122
int32_t oldStart = position;
1124
// check for positivePrefix; take longest
1125
UBool gotPositive = text.compare(position,fPositivePrefix.length(),fPositivePrefix,0,
1126
fPositivePrefix.length()) == 0;
1127
UBool gotNegative = text.compare(position,fNegativePrefix.length(),fNegativePrefix,0,
1128
fNegativePrefix.length()) == 0;
1129
// If the number is positive and negative at the same time,
1130
// 1. the number is positive if the positive prefix is longer
1131
// 2. the number is negative if the negative prefix is longer
1132
if (gotPositive && gotNegative) {
1133
if (fPositivePrefix.length() > fNegativePrefix.length())
1134
gotNegative = FALSE;
1135
else if (fPositivePrefix.length() < fNegativePrefix.length())
1136
gotPositive = FALSE;
1139
position += fPositivePrefix.length();
1140
else if(gotNegative)
1141
position += fNegativePrefix.length();
1143
parsePosition.setErrorIndex(position);
1147
// process digits or Inf, find decimal position
1148
UnicodeString inf(fSymbols->getSymbol(DecimalFormatSymbols::kInfinitySymbol));
1149
int32_t infLen = (text.compare(position, inf.length(), inf)
1150
? 0 : inf.length());
1151
position += infLen; // infLen is non-zero when it does equal to infinity
1152
status[fgStatusInfinite] = (UBool)infLen;
1155
// We now have a string of digits, possibly with grouping symbols,
1156
// and decimal points. We want to process these into a DigitList.
1157
// We don't want to put a bunch of leading zeros into the DigitList
1158
// though, so we keep track of the location of the decimal point,
1159
// put only significant digits into the DigitList, and adjust the
1160
// exponent as needed.
1162
digits.fDecimalAt = digits.fCount = 0;
1163
UChar32 zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
1164
UnicodeString decimal(fIsCurrencyFormat
1165
? fSymbols->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol)
1166
: fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
1167
UnicodeString grouping(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
1168
UnicodeString exponentChar(fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol));
1169
UBool sawDecimal = FALSE;
1170
UBool sawDigit = FALSE;
1171
int32_t backup = -1;
1174
int32_t textLength = text.length(); // One less pointer to follow
1175
int32_t groupingLen = grouping.length();
1176
int32_t decimalLen = decimal.length();
1178
// We have to track digitCount ourselves, because digits.fCount will
1179
// pin when the maximum allowable digits is reached.
1180
int32_t digitCount = 0;
1182
for (; position < textLength; position += 1 + UTF_NEED_MULTIPLE_UCHAR(ch))
1184
ch = text.char32At(position);
1186
/* We recognize all digit ranges, not only the Latin digit range
1187
* '0'..'9'. We do so by using the Character.digit() method,
1188
* which converts a valid Unicode digit to the range 0..9.
1190
* The character 'ch' may be a digit. If so, place its value
1191
* from 0 to 9 in 'digit'. First try using the locale digit,
1192
* which may or MAY NOT be a standard Unicode digit range. If
1193
* this fails, try using the standard Unicode digit ranges by
1194
* calling Character.digit(). If this also fails, digit will
1195
* have a value outside the range 0..9.
1198
if (digit < 0 || digit > 9)
1200
digit = u_charDigitValue(ch);
1203
if (digit > 0 && digit <= 9)
1205
// Cancel out backup setting (see grouping handler below)
1209
// output a regular non-zero digit.
1211
digits.append((char)(digit + '0'));
1213
else if (digit == 0)
1215
// Cancel out backup setting (see grouping handler below)
1219
// Check for leading zeros
1220
if (digits.fCount != 0)
1222
// output a regular zero digit.
1224
digits.append((char)(digit + '0'));
1226
else if (sawDecimal)
1228
// If we have seen the decimal, but no significant digits yet,
1229
// then we account for leading zeros by decrementing the
1230
// digits.fDecimalAt into negative values.
1231
--digits.fDecimalAt;
1233
// else ignore leading zeros in integer part of number.
1235
else if (!text.compare(position, groupingLen, grouping) && isGroupingUsed())
1237
// Ignore grouping characters, if we are using them, but require
1238
// that they be followed by a digit. Otherwise we backup and
1242
else if (!text.compare(position, decimalLen, decimal) && !isParseIntegerOnly() && !sawDecimal)
1244
// If we're only parsing integers, or if we ALREADY saw the
1245
// decimal, then don't parse this one.
1247
digits.fDecimalAt = digitCount; // Not digits.fCount!
1250
else if (!text.caseCompare(position,
1251
fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol).length(),
1252
fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol),
1253
U_FOLD_CASE_DEFAULT)) // error code is set below if !sawDigit
1255
// Parse sign, if present
1256
int32_t pos = position + 1; // position + exponentSep.length();
1257
DigitList exponentDigits;
1259
if (pos < textLength)
1261
if (!text.compare(pos,
1262
fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol).length(),
1263
fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol)))
1267
else if (!text.compare(pos,
1268
fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol).length(),
1269
fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)))
1272
exponentDigits.fIsPositive = FALSE;
1276
while (pos < textLength) {
1277
ch = text[(int32_t)pos];
1280
if (digit < 0 || digit > 9) {
1281
digit = u_charDigitValue(ch);
1283
if (0 <= digit && digit <= 9) {
1285
exponentDigits.append((char)(digit + '0'));
1291
if (exponentDigits.fCount > 0) {
1292
exponentDigits.fDecimalAt = exponentDigits.fCount;
1293
digits.fDecimalAt += exponentDigits.getLong();
1294
position = pos; // Advance past the exponent
1297
break; // Whether we fail or succeed, we exit this loop
1308
// If there was no decimal point we have an integer
1311
digits.fDecimalAt += digitCount; // Not digits.fCount!
1314
// If none of the text string was recognized. For example, parse
1315
// "x" with pattern "#0.00" (return index and error index both 0)
1316
// parse "$" with pattern "$#0.00". (return index 0 and error index
1318
if (!sawDigit && digitCount == 0) {
1319
parsePosition.setIndex(oldStart);
1320
parsePosition.setErrorIndex(oldStart);
1325
// check for positiveSuffix
1326
if (gotPositive && fPositiveSuffix.length() > 0)
1328
gotPositive = text.compare(position,fPositiveSuffix.length(),fPositiveSuffix,0,
1329
fPositiveSuffix.length()) == 0;
1331
if (gotNegative && fNegativeSuffix.length() > 0)
1333
gotNegative = text.compare(position,fNegativeSuffix.length(),fNegativeSuffix,0,
1334
fNegativeSuffix.length()) == 0;
1337
// if both match, take longest
1338
if (gotPositive && gotNegative)
1340
if (fPositiveSuffix.length() > fNegativeSuffix.length())
1342
gotNegative = FALSE;
1344
else if (fPositiveSuffix.length() < fNegativeSuffix.length())
1346
gotPositive = FALSE;
1350
gotPositive = TRUE; // Make them equal to each other.
1355
// fail if neither or both
1356
if (gotPositive == gotNegative)
1358
parsePosition.setErrorIndex(position);
1362
parsePosition.setIndex(position +
1363
(gotPositive ? fPositiveSuffix.length() :
1364
fNegativeSuffix.length())); // mark success!
1366
digits.fIsPositive = gotPositive;
1368
if(parsePosition.getIndex() == oldStart)
1370
parsePosition.setErrorIndex(position);
1377
//------------------------------------------------------------------------------
1378
// Gets the pointer to the localized decimal format symbols
1380
const DecimalFormatSymbols*
1381
DecimalFormat::getDecimalFormatSymbols() const
1386
//------------------------------------------------------------------------------
1387
// De-owning the current localized symbols and adopt the new symbols.
1390
DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
1392
if (fSymbols != NULL)
1395
fSymbols = symbolsToAdopt;
1397
//------------------------------------------------------------------------------
1398
// Setting the symbols is equlivalent to adopting a newly created localized
1402
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
1404
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
1408
//------------------------------------------------------------------------------
1409
// Gets the positive prefix of the number pattern.
1412
DecimalFormat::getPositivePrefix(UnicodeString& result) const
1414
result = fPositivePrefix;
1418
//------------------------------------------------------------------------------
1419
// Sets the positive prefix of the number pattern.
1422
DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
1424
fPositivePrefix = newValue;
1425
delete fPosPrefixPattern;
1426
fPosPrefixPattern = 0;
1429
//------------------------------------------------------------------------------
1430
// Gets the negative prefix of the number pattern.
1433
DecimalFormat::getNegativePrefix(UnicodeString& result) const
1435
result = fNegativePrefix;
1439
//------------------------------------------------------------------------------
1440
// Gets the negative prefix of the number pattern.
1443
DecimalFormat::setNegativePrefix(const UnicodeString& newValue)
1445
fNegativePrefix = newValue;
1446
delete fNegPrefixPattern;
1447
fNegPrefixPattern = 0;
1450
//------------------------------------------------------------------------------
1451
// Gets the positive suffix of the number pattern.
1454
DecimalFormat::getPositiveSuffix(UnicodeString& result) const
1456
result = fPositiveSuffix;
1460
//------------------------------------------------------------------------------
1461
// Sets the positive suffix of the number pattern.
1464
DecimalFormat::setPositiveSuffix(const UnicodeString& newValue)
1466
fPositiveSuffix = newValue;
1467
delete fPosSuffixPattern;
1468
fPosSuffixPattern = 0;
1471
//------------------------------------------------------------------------------
1472
// Gets the negative suffix of the number pattern.
1475
DecimalFormat::getNegativeSuffix(UnicodeString& result) const
1477
result = fNegativeSuffix;
1481
//------------------------------------------------------------------------------
1482
// Sets the negative suffix of the number pattern.
1485
DecimalFormat::setNegativeSuffix(const UnicodeString& newValue)
1487
fNegativeSuffix = newValue;
1488
delete fNegSuffixPattern;
1489
fNegSuffixPattern = 0;
1492
//------------------------------------------------------------------------------
1493
// Gets the multiplier of the number pattern.
1495
int32_t DecimalFormat::getMultiplier() const
1500
//------------------------------------------------------------------------------
1501
// Sets the multiplier of the number pattern.
1503
DecimalFormat::setMultiplier(int32_t newValue)
1505
// This shouldn't be set to 0.
1506
// Due to compatibility with ICU4J we cannot set an error code and refuse 0.
1507
// So the rest of the code should ignore fMultiplier when it's 0. [grhoten]
1508
fMultiplier = newValue;
1512
* Get the rounding increment.
1513
* @return A positive rounding increment, or 0.0 if rounding
1515
* @see #setRoundingIncrement
1516
* @see #getRoundingMode
1517
* @see #setRoundingMode
1519
double DecimalFormat::getRoundingIncrement() {
1520
return fRoundingDouble;
1524
* Set the rounding increment. This method also controls whether
1525
* rounding is enabled.
1526
* @param newValue A positive rounding increment, or 0.0 to disable rounding.
1527
* Negative increments are equivalent to 0.0.
1528
* @see #getRoundingIncrement
1529
* @see #getRoundingMode
1530
* @see #setRoundingMode
1532
void DecimalFormat::setRoundingIncrement(double newValue) {
1533
if (newValue > 0.0) {
1534
if (fRoundingIncrement == NULL) {
1535
fRoundingIncrement = new DigitList();
1537
fRoundingIncrement->set((int32_t)newValue);
1538
fRoundingDouble = newValue;
1540
delete fRoundingIncrement;
1541
fRoundingIncrement = NULL;
1542
fRoundingDouble = 0.0;
1547
* Get the rounding mode.
1548
* @return A rounding mode
1549
* @see #setRoundingIncrement
1550
* @see #getRoundingIncrement
1551
* @see #setRoundingMode
1553
DecimalFormat::ERoundingMode DecimalFormat::getRoundingMode() {
1554
return fRoundingMode;
1558
* Set the rounding mode. This has no effect unless the rounding
1559
* increment is greater than zero.
1560
* @param roundingMode A rounding mode
1561
* @see #setRoundingIncrement
1562
* @see #getRoundingIncrement
1563
* @see #getRoundingMode
1565
void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
1566
fRoundingMode = roundingMode;
1570
* Get the width to which the output of <code>format()</code> is padded.
1571
* @return the format width, or zero if no padding is in effect
1572
* @see #setFormatWidth
1573
* @see #getPadCharacter
1574
* @see #setPadCharacter
1575
* @see #getPadPosition
1576
* @see #setPadPosition
1578
int32_t DecimalFormat::getFormatWidth() {
1579
return fFormatWidth;
1583
* Set the width to which the output of <code>format()</code> is padded.
1584
* This method also controls whether padding is enabled.
1585
* @param width the width to which to pad the result of
1586
* <code>format()</code>, or zero to disable padding. A negative
1587
* width is equivalent to 0.
1588
* @see #getFormatWidth
1589
* @see #getPadCharacter
1590
* @see #setPadCharacter
1591
* @see #getPadPosition
1592
* @see #setPadPosition
1594
void DecimalFormat::setFormatWidth(int32_t width) {
1595
fFormatWidth = (width > 0) ? width : 0;
1599
* Get the character used to pad to the format width. The default is ' '.
1600
* @return the pad character
1601
* @see #setFormatWidth
1602
* @see #getFormatWidth
1603
* @see #setPadCharacter
1604
* @see #getPadPosition
1605
* @see #setPadPosition
1607
UnicodeString DecimalFormat::getPadCharacterString() {
1612
* Set the character used to pad to the format width. This has no effect
1613
* unless padding is enabled.
1614
* @param padChar the pad character
1615
* @see #setFormatWidth
1616
* @see #getFormatWidth
1617
* @see #getPadCharacter
1618
* @see #getPadPosition
1619
* @see #setPadPosition
1621
void DecimalFormat::setPadCharacter(UnicodeString padChar) {
1622
if (padChar.length() > 0) {
1626
fPad = kPatternPadEscape;
1631
* Get the position at which padding will take place. This is the location
1632
* at which padding will be inserted if the result of <code>format()</code>
1633
* is shorter than the format width.
1634
* @return the pad position, one of <code>kPadBeforePrefix</code>,
1635
* <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
1636
* <code>kPadAfterSuffix</code>.
1637
* @see #setFormatWidth
1638
* @see #getFormatWidth
1639
* @see #setPadCharacter
1640
* @see #getPadCharacter
1641
* @see #setPadPosition
1642
* @see #kPadBeforePrefix
1643
* @see #kPadAfterPrefix
1644
* @see #kPadBeforeSuffix
1645
* @see #kPadAfterSuffix
1647
DecimalFormat::EPadPosition DecimalFormat::getPadPosition() {
1648
return fPadPosition;
1652
* <strong><font face=helvetica color=red>NEW</font></strong>
1653
* Set the position at which padding will take place. This is the location
1654
* at which padding will be inserted if the result of <code>format()</code>
1655
* is shorter than the format width. This has no effect unless padding is
1657
* @param padPos the pad position, one of <code>kPadBeforePrefix</code>,
1658
* <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
1659
* <code>kPadAfterSuffix</code>.
1660
* @see #setFormatWidth
1661
* @see #getFormatWidth
1662
* @see #setPadCharacter
1663
* @see #getPadCharacter
1664
* @see #getPadPosition
1665
* @see #kPadBeforePrefix
1666
* @see #kPadAfterPrefix
1667
* @see #kPadBeforeSuffix
1668
* @see #kPadAfterSuffix
1670
void DecimalFormat::setPadPosition(EPadPosition padPos) {
1671
fPadPosition = padPos;
1675
* Return whether or not scientific notation is used.
1676
* @return TRUE if this object formats and parses scientific notation
1677
* @see #setScientificNotation
1678
* @see #getMinimumExponentDigits
1679
* @see #setMinimumExponentDigits
1680
* @see #isExponentSignAlwaysShown
1681
* @see #setExponentSignAlwaysShown
1683
UBool DecimalFormat::isScientificNotation() {
1684
return fUseExponentialNotation;
1688
* Set whether or not scientific notation is used.
1689
* @param useScientific TRUE if this object formats and parses scientific
1691
* @see #isScientificNotation
1692
* @see #getMinimumExponentDigits
1693
* @see #setMinimumExponentDigits
1694
* @see #isExponentSignAlwaysShown
1695
* @see #setExponentSignAlwaysShown
1697
void DecimalFormat::setScientificNotation(UBool useScientific) {
1698
fUseExponentialNotation = useScientific;
1699
if (fUseExponentialNotation && fMinExponentDigits < 1) {
1700
fMinExponentDigits = 1;
1705
* Return the minimum exponent digits that will be shown.
1706
* @return the minimum exponent digits that will be shown
1707
* @see #setScientificNotation
1708
* @see #isScientificNotation
1709
* @see #setMinimumExponentDigits
1710
* @see #isExponentSignAlwaysShown
1711
* @see #setExponentSignAlwaysShown
1713
int8_t DecimalFormat::getMinimumExponentDigits() {
1714
return fMinExponentDigits;
1718
* Set the minimum exponent digits that will be shown. This has no
1719
* effect unless scientific notation is in use.
1720
* @param minExpDig a value >= 1 indicating the fewest exponent digits
1721
* that will be shown. Values less than 1 will be treated as 1.
1722
* @see #setScientificNotation
1723
* @see #isScientificNotation
1724
* @see #getMinimumExponentDigits
1725
* @see #isExponentSignAlwaysShown
1726
* @see #setExponentSignAlwaysShown
1728
void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
1729
fMinExponentDigits = (int8_t)((minExpDig > 0) ? minExpDig : 1);
1733
* Return whether the exponent sign is always shown.
1734
* @return TRUE if the exponent is always prefixed with either the
1735
* localized minus sign or the localized plus sign, false if only negative
1736
* exponents are prefixed with the localized minus sign.
1737
* @see #setScientificNotation
1738
* @see #isScientificNotation
1739
* @see #setMinimumExponentDigits
1740
* @see #getMinimumExponentDigits
1741
* @see #setExponentSignAlwaysShown
1743
UBool DecimalFormat::isExponentSignAlwaysShown() {
1744
return fExponentSignAlwaysShown;
1748
* Set whether the exponent sign is always shown. This has no effect
1749
* unless scientific notation is in use.
1750
* @param expSignAlways TRUE if the exponent is always prefixed with either
1751
* the localized minus sign or the localized plus sign, false if only
1752
* negative exponents are prefixed with the localized minus sign.
1753
* @see #setScientificNotation
1754
* @see #isScientificNotation
1755
* @see #setMinimumExponentDigits
1756
* @see #getMinimumExponentDigits
1757
* @see #isExponentSignAlwaysShown
1759
void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
1760
fExponentSignAlwaysShown = expSignAlways;
1763
//------------------------------------------------------------------------------
1764
// Gets the grouping size of the number pattern. For example, thousand or 10
1765
// thousand groupings.
1768
DecimalFormat::getGroupingSize() const
1770
return fGroupingSize;
1773
//------------------------------------------------------------------------------
1774
// Gets the grouping size of the number pattern.
1777
DecimalFormat::setGroupingSize(int32_t newValue)
1779
fGroupingSize = newValue;
1782
//------------------------------------------------------------------------------
1785
DecimalFormat::getSecondaryGroupingSize() const
1787
return fGroupingSize2;
1790
//------------------------------------------------------------------------------
1793
DecimalFormat::setSecondaryGroupingSize(int32_t newValue)
1795
fGroupingSize2 = newValue;
1798
//------------------------------------------------------------------------------
1799
// Checks if to show the decimal separator.
1802
DecimalFormat::isDecimalSeparatorAlwaysShown() const
1804
return fDecimalSeparatorAlwaysShown;
1807
//------------------------------------------------------------------------------
1808
// Sets to always show the decimal separator.
1811
DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue)
1813
fDecimalSeparatorAlwaysShown = newValue;
1816
//------------------------------------------------------------------------------
1817
// Emits the pattern of this DecimalFormat instance.
1820
DecimalFormat::toPattern(UnicodeString& result) const
1822
return toPattern(result, FALSE);
1825
//------------------------------------------------------------------------------
1826
// Emits the localized pattern this DecimalFormat instance.
1829
DecimalFormat::toLocalizedPattern(UnicodeString& result) const
1831
return toPattern(result, TRUE);
1834
//------------------------------------------------------------------------------
1836
* Expand the affix pattern strings into the expanded affix strings. If any
1837
* affix pattern string is null, do not expand it. This method should be
1838
* called any time the symbols or the affix patterns change in order to keep
1839
* the expanded affix strings up to date.
1841
void DecimalFormat::expandAffixes(void) {
1842
if (fPosPrefixPattern != 0) {
1843
expandAffix(*fPosPrefixPattern, fPositivePrefix);
1845
if (fPosSuffixPattern != 0) {
1846
expandAffix(*fPosSuffixPattern, fPositiveSuffix);
1848
if (fNegPrefixPattern != 0) {
1849
expandAffix(*fNegPrefixPattern, fNegativePrefix);
1851
if (fNegSuffixPattern != 0) {
1852
expandAffix(*fNegSuffixPattern, fNegativeSuffix);
1857
.append(*fPosPrefixPattern).append("|").append(*fPosSuffixPattern)
1858
.append(";") .append(*fNegPrefixPattern).append("|").append(*fNegSuffixPattern)
1860
.append(fPositivePrefix).append("|").append(fPositiveSuffix)
1861
.append(";") .append(fNegativePrefix).append("|").append(fNegativeSuffix)
1868
* Expand an affix pattern into an affix string. All characters in the
1869
* pattern are literal unless prefixed by kQuote. The following characters
1870
* after kQuote are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
1871
* PATTERN_MINUS, and kCurrencySign. If kCurrencySign is doubled (kQuote +
1872
* kCurrencySign + kCurrencySign), it is interpreted as an international
1873
* currency sign. Any other character after a kQuote represents itself.
1874
* kQuote must be followed by another character; kQuote may not occur by
1875
* itself at the end of the pattern.
1877
* @param pattern the non-null, fPossibly empty pattern
1878
* @param affix string to receive the expanded equivalent of pattern
1880
void DecimalFormat::expandAffix(const UnicodeString& pattern,
1881
UnicodeString& affix) const {
1883
for (int i=0; i<pattern.length(); ) {
1884
UChar32 c = pattern.char32At(i++);
1886
c = pattern.char32At(i++);
1890
if (i<pattern.length() &&
1891
pattern.char32At(i) == kCurrencySign) {
1893
affix += fSymbols->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
1895
affix += fSymbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol);
1899
case kPatternPercent:
1900
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol));
1902
case kPatternPerMill:
1903
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol));
1906
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
1909
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol));
1923
* Appends an affix pattern to the given StringBuffer, quoting special
1924
* characters as needed. Uses the internal affix pattern, if that exists,
1925
* or the literal affix, if the internal affix pattern is null. The
1926
* appended string will generate the same affix pattern (or literal affix)
1927
* when passed to toPattern().
1929
* @param buffer the affix string is appended to this
1930
* @param affixPattern a pattern such as fPosPrefixPattern; may be null
1931
* @param expAffix a corresponding expanded affix, such as fPositivePrefix.
1932
* Ignored unless affixPattern is null. If affixPattern is null, then
1933
* expAffix is appended as a literal affix.
1934
* @param localized true if the appended pattern should contain localized
1935
* pattern characters; otherwise, non-localized pattern chars are appended
1937
void DecimalFormat::appendAffix(UnicodeString& buffer,
1938
const UnicodeString* affixPattern,
1939
const UnicodeString& expAffix,
1940
UBool localized) const {
1941
if (affixPattern == 0) {
1942
appendAffix(buffer, expAffix, localized);
1945
for (int pos=0; pos<affixPattern->length(); pos=i) {
1946
i = affixPattern->indexOf(kQuote, pos);
1949
affixPattern->extractBetween(pos, affixPattern->length(), s);
1950
appendAffix(buffer, s, localized);
1955
affixPattern->extractBetween(pos, i, s);
1956
appendAffix(buffer, s, localized);
1958
UChar32 c = affixPattern->char32At(++i);
1961
buffer.append(c).append(c);
1962
// Fall through and append another kQuote below
1963
} else if (c == kCurrencySign &&
1964
i<affixPattern->length() &&
1965
affixPattern->char32At(i) == kCurrencySign) {
1967
buffer.append(c).append(c);
1968
} else if (localized) {
1970
case kPatternPercent:
1971
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol));
1973
case kPatternPerMill:
1974
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol));
1977
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
1980
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol));
1993
* Append an affix to the given StringBuffer, using quotes if
1994
* there are special characters. Single quotes themselves must be
1995
* escaped in either case.
1998
DecimalFormat::appendAffix( UnicodeString& buffer,
1999
const UnicodeString& affix,
2000
UBool localized) const {
2003
needQuote = affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol)) >= 0
2004
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol)) >= 0
2005
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol)) >= 0
2006
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol)) >= 0
2007
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol)) >= 0
2008
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol)) >= 0
2009
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol)) >= 0
2010
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol)) >= 0
2011
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)) >= 0
2012
|| affix.indexOf(kCurrencySign) >= 0;
2015
needQuote = affix.indexOf(kPatternZeroDigit) >= 0
2016
|| affix.indexOf(kPatternGroupingSeparator) >= 0
2017
|| affix.indexOf(kPatternDecimalSeparator) >= 0
2018
|| affix.indexOf(kPatternPercent) >= 0
2019
|| affix.indexOf(kPatternPerMill) >= 0
2020
|| affix.indexOf(kPatternDigit) >= 0
2021
|| affix.indexOf(kPatternSeparator) >= 0
2022
|| affix.indexOf(kPatternExponent) >= 0
2023
|| affix.indexOf(kPatternPlus) >= 0
2024
|| affix.indexOf(kPatternMinus) >= 0
2025
|| affix.indexOf(kCurrencySign) >= 0;
2028
buffer += (UChar)0x0027 /*'\''*/;
2029
if (affix.indexOf((UChar)0x0027 /*'\''*/) < 0)
2032
for (int32_t j = 0; j < affix.length(); ++j) {
2033
UChar32 c = affix.char32At(j);
2035
if (c == 0x0027 /*'\''*/)
2037
j = j + UTF_NEED_MULTIPLE_UCHAR(c);
2041
buffer += (UChar)0x0027 /*'\''*/;
2044
//------------------------------------------------------------------------------
2046
/* Tell the VC++ compiler not to spew out the warnings about integral size conversion */
2049
#pragma warning( disable : 4761 )
2054
DecimalFormat::toPattern(UnicodeString& result, UBool localized) const
2058
UnicodeString digit;
2059
UnicodeString group;
2061
int32_t roundingDecimalPos = 0; // Pos of decimal in roundingDigits
2062
UnicodeString roundingDigits;
2063
int32_t padPos = (fFormatWidth > 0) ? fPadPosition : -1;
2064
UnicodeString padSpec;
2067
digit = fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol);
2068
group = fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
2069
zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
2072
digit.append((UChar)kPatternDigit);
2073
group.append((UChar)kPatternGroupingSeparator);
2074
zero = (UChar32)kPatternZeroDigit;
2076
if (fFormatWidth > 0) {
2078
padSpec.append(fSymbols->getSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
2081
padSpec.append((UChar)kPatternPadEscape);
2083
padSpec.append(fPad);
2085
if (fRoundingIncrement != NULL) {
2086
for(i=0; i<fRoundingIncrement->fCount; ++i) {
2087
roundingDigits.append((UChar)fRoundingIncrement->fDigits[i]);
2089
roundingDecimalPos = fRoundingIncrement->fDecimalAt;
2091
for (int32_t part=0; part<2; ++part) {
2092
if (padPos == kPadBeforePrefix) {
2093
result.append(padSpec);
2096
(part==0 ? fPosPrefixPattern : fNegPrefixPattern),
2097
(part==0 ? fPositivePrefix : fNegativePrefix),
2099
if (padPos == kPadAfterPrefix && ! padSpec.isEmpty()) {
2100
result.append(padSpec);
2102
int32_t sub0Start = result.length();
2103
int32_t g = isGroupingUsed() ? uprv_max(0, fGroupingSize) : 0;
2104
if (g > 0 && fGroupingSize2 > 0 && fGroupingSize2 != fGroupingSize) {
2105
g += fGroupingSize2;
2107
int32_t maxIntDig = fUseExponentialNotation ? getMaximumIntegerDigits() :
2108
(uprv_max(uprv_max(g, getMinimumIntegerDigits()),
2109
roundingDecimalPos) + 1);
2110
for (i = maxIntDig; i > 0; --i) {
2111
if (!fUseExponentialNotation && i<maxIntDig &&
2112
isGroupingPosition(i)) {
2113
result.append(group);
2115
if (! roundingDigits.isEmpty()) {
2116
int32_t pos = roundingDecimalPos - i;
2117
if (pos >= 0 && pos < roundingDigits.length()) {
2118
result.append((UChar) (roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
2122
if (i<=getMinimumIntegerDigits()) {
2123
result.append(zero);
2126
result.append(digit);
2129
if (getMaximumFractionDigits() > 0 || fDecimalSeparatorAlwaysShown) {
2131
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
2134
result.append((UChar)kPatternDecimalSeparator);
2137
int32_t pos = roundingDecimalPos;
2138
for (i = 0; i < getMaximumFractionDigits(); ++i) {
2139
if (! roundingDigits.isEmpty() && pos < roundingDigits.length()) {
2141
result.append(zero);
2144
result.append((UChar)(roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
2149
if (i<getMinimumFractionDigits()) {
2150
result.append(zero);
2153
result.append(digit);
2156
if (fUseExponentialNotation) {
2158
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol));
2161
result.append((UChar)kPatternExponent);
2163
if (fExponentSignAlwaysShown) {
2165
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
2168
result.append((UChar)kPatternPlus);
2171
for (i=0; i<fMinExponentDigits; ++i) {
2172
result.append(zero);
2175
if (! padSpec.isEmpty() && !fUseExponentialNotation) {
2176
int32_t add = fFormatWidth - result.length() + sub0Start
2178
? fPositivePrefix.length() + fPositiveSuffix.length()
2179
: fNegativePrefix.length() + fNegativeSuffix.length());
2181
result.insert(sub0Start, digit);
2184
// Only add a grouping separator if we have at least
2185
// 2 additional characters to be added, so we don't
2186
// end up with ",###".
2187
if (add>1 && isGroupingPosition(maxIntDig)) {
2188
result.insert(sub0Start, group);
2193
if (fPadPosition == kPadBeforeSuffix && ! padSpec.isEmpty()) {
2194
result.append(padSpec);
2197
appendAffix(result, fPosSuffixPattern, fPositiveSuffix, localized);
2198
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
2199
result.append(padSpec);
2201
UBool isDefault = FALSE;
2202
if ((fNegSuffixPattern == fPosSuffixPattern && // both null
2203
fNegativeSuffix == fPositiveSuffix)
2204
|| (fNegSuffixPattern != 0 && fPosSuffixPattern != 0 &&
2205
*fNegSuffixPattern == *fPosSuffixPattern))
2207
if (fNegPrefixPattern != NULL && fPosPrefixPattern != NULL)
2209
int32_t length = fPosPrefixPattern->length();
2210
isDefault = fNegPrefixPattern->length() == (length+2) &&
2211
(*fNegPrefixPattern)[(int32_t)0] == kQuote &&
2212
(*fNegPrefixPattern)[(int32_t)1] == kPatternMinus &&
2213
fNegPrefixPattern->compare(2, length, *fPosPrefixPattern, 0, length) == 0;
2216
fNegPrefixPattern == NULL && fPosPrefixPattern == NULL)
2218
int32_t length = fPositivePrefix.length();
2219
isDefault = fNegativePrefix.length() == (length+1) &&
2220
fNegativePrefix.compare(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)) == 0 &&
2221
fNegativePrefix.compare(1, length, fPositivePrefix, 0, length) == 0;
2225
break; // Don't output default negative subpattern
2228
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
2231
result.append((UChar)kPatternSeparator);
2235
appendAffix(result, fNegSuffixPattern, fNegativeSuffix, localized);
2236
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
2237
result.append(padSpec);
2245
//------------------------------------------------------------------------------
2248
DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
2250
UParseError parseError;
2251
applyPattern(pattern, FALSE, parseError, status);
2254
//------------------------------------------------------------------------------
2257
DecimalFormat::applyPattern(const UnicodeString& pattern,
2258
UParseError& parseError,
2261
applyPattern(pattern, FALSE, parseError, status);
2263
//------------------------------------------------------------------------------
2266
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
2268
UParseError parseError;
2269
applyPattern(pattern, TRUE,parseError,status);
2272
//------------------------------------------------------------------------------
2275
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
2276
UParseError& parseError,
2279
applyPattern(pattern, TRUE,parseError,status);
2282
//------------------------------------------------------------------------------
2285
DecimalFormat::applyPattern(const UnicodeString& pattern,
2287
UParseError& parseError,
2290
if (U_FAILURE(status))
2294
// Clear error struct
2295
parseError.offset = -1;
2296
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
2298
// Set the significant pattern symbols
2299
UChar32 zeroDigit = kPatternZeroDigit;
2300
UnicodeString groupingSeparator ((UChar)kPatternGroupingSeparator);
2301
UnicodeString decimalSeparator ((UChar)kPatternDecimalSeparator);
2302
UnicodeString percent ((UChar)kPatternPercent);
2303
UnicodeString perMill ((UChar)kPatternPerMill);
2304
UnicodeString digit ((UChar)kPatternDigit);
2305
UnicodeString separator ((UChar)kPatternSeparator);
2306
UnicodeString exponent ((UChar)kPatternExponent);
2307
UnicodeString plus ((UChar)kPatternPlus);
2308
UnicodeString minus ((UChar)kPatternMinus);
2309
UnicodeString padEscape ((UChar)kPatternPadEscape);
2310
// Substitute with the localized symbols if necessary
2312
zeroDigit = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
2313
groupingSeparator = fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
2314
decimalSeparator = fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
2315
percent = fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol);
2316
perMill = fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol);
2317
digit = fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol);
2318
separator = fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol);
2319
exponent = fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol);
2320
plus = fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol);
2321
minus = fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol);
2322
padEscape = fSymbols->getSymbol(DecimalFormatSymbols::kPadEscapeSymbol);
2324
UChar nineDigit = (UChar)(zeroDigit + 9);
2325
int32_t digitLen = digit.length();
2326
int32_t groupSepLen = groupingSeparator.length();
2327
int32_t decimalSepLen = decimalSeparator.length();
2330
int32_t patLen = pattern.length();
2331
// Part 0 is the positive pattern. Part 1, if present, is the negative
2333
for (int32_t part=0; part<2 && pos<patLen; ++part) {
2334
// The subpart ranges from 0 to 4: 0=pattern proper, 1=prefix,
2335
// 2=suffix, 3=prefix in quote, 4=suffix in quote. Subpart 0 is
2336
// between the prefix and suffix, and consists of pattern
2337
// characters. In the prefix and suffix, percent, perMill, and
2338
// currency symbols are recognized and translated.
2339
int32_t subpart = 1, sub0Start = 0, sub0Limit = 0, sub2Limit = 0;
2341
// It's important that we don't change any fields of this object
2342
// prematurely. We set the following variables for the multiplier,
2343
// grouping, etc., and then only change the actual object fields if
2344
// everything parses correctly. This also lets us register
2345
// the data from part 0 and ignore the part 1, except for the
2346
// prefix and suffix.
2347
UnicodeString prefix;
2348
UnicodeString suffix;
2349
int32_t decimalPos = -1;
2350
int32_t multiplier = 1;
2351
int32_t digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0;
2352
int8_t groupingCount = -1;
2353
int8_t groupingCount2 = -1;
2354
int32_t padPos = -1;
2355
UnicodeString padChar;
2356
int32_t roundingPos = -1;
2357
DigitList roundingInc;
2358
int8_t expDigits = -1;
2359
UBool expSignAlways = FALSE;
2360
UBool isCurrency = FALSE;
2362
// The affix is either the prefix or the suffix.
2363
UnicodeString* affix = &prefix;
2365
int32_t start = pos;
2366
UBool isPartDone = FALSE;
2369
for (; !isPartDone && pos < patLen; pos += UTF_NEED_MULTIPLE_UCHAR(ch)) {
2370
// Todo: account for surrogate pairs
2371
ch = pattern.char32At(pos);
2373
case 0: // Pattern proper subpart (between prefix & suffix)
2374
// Process the digits, decimal, and grouping characters. We
2375
// record five pieces of information. We expect the digits
2376
// to occur in the pattern ####00.00####, and we record the
2377
// number of left digits, zero (central) digits, and right
2378
// digits. The position of the last grouping character is
2379
// recorded (should be somewhere within the first two blocks
2380
// of characters), as is the position of the decimal point,
2381
// if any (should be in the zero digits). If there is no
2382
// decimal point, then there should be no right digits.
2383
if (pattern.compare(pos, digitLen, digit) == 0) {
2384
if (zeroDigitCount > 0) {
2389
if (groupingCount >= 0 && decimalPos < 0) {
2393
} else if (ch >= zeroDigit && ch <= nineDigit) {
2394
if (digitRightCount > 0) {
2396
debug("Unexpected '0'")
2397
status = U_UNEXPECTED_TOKEN;
2398
syntaxError(pattern,pos,parseError);
2402
if (groupingCount >= 0 && decimalPos < 0) {
2405
if (ch != zeroDigit && roundingPos < 0) {
2406
roundingPos = digitLeftCount + zeroDigitCount;
2408
if (roundingPos >= 0) {
2409
roundingInc.append((char)(ch - zeroDigit + '0'));
2412
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
2413
if (decimalPos >= 0) {
2414
// Grouping separator after decimal
2415
debug("Grouping separator after decimal")
2416
status = U_UNEXPECTED_TOKEN;
2417
syntaxError(pattern,pos,parseError);
2420
groupingCount2 = groupingCount;
2423
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
2424
if (decimalPos >= 0) {
2425
// Multiple decimal separators
2426
debug("Multiple decimal separators")
2427
status = U_MULTIPLE_DECIMAL_SEPERATORS;
2428
syntaxError(pattern,pos,parseError);
2431
// Intentionally incorporate the digitRightCount,
2432
// even though it is illegal for this to be > 0
2433
// at this point. We check pattern syntax below.
2434
decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
2435
pos += decimalSepLen;
2437
if (pattern.compare(pos, exponent.length(), exponent) == 0) {
2438
if (expDigits >= 0) {
2439
// Multiple exponential symbols
2440
debug("Multiple exponential symbols")
2441
status = U_MULTIPLE_EXPONENTIAL_SYMBOLS;
2442
syntaxError(pattern,pos,parseError);
2445
if (groupingCount >= 0) {
2446
// Grouping separator in exponential pattern
2447
debug("Grouping separator in exponential pattern")
2448
status = U_MALFORMED_EXPONENTIAL_PATTERN;
2449
syntaxError(pattern,pos,parseError);
2452
// Check for positive prefix
2453
if ((pos+1) < patLen
2454
&& pattern.compare((int32_t) (pos+1), plus.length(), plus) == 0)
2456
expSignAlways = TRUE;
2457
pos += plus.length();
2459
// Use lookahead to parse out the exponential part of the
2460
// pattern, then jump into suffix subpart.
2462
pos += exponent.length() - 1;
2463
while (++pos < patLen &&
2464
pattern[(int32_t) pos] == zeroDigit)
2469
if ((digitLeftCount + zeroDigitCount) < 1 ||
2471
// Malformed exponential pattern
2472
debug("Malformed exponential pattern")
2473
status = U_MALFORMED_EXPONENTIAL_PATTERN;
2474
syntaxError(pattern,pos,parseError);
2478
// Transition to suffix subpart
2479
subpart = 2; // suffix subpart
2485
case 1: // Prefix subpart
2486
case 2: // Suffix subpart
2487
// Process the prefix / suffix characters
2488
// Process unquoted characters seen in prefix or suffix
2490
if (pattern.compare(pos, digitLen, digit) == 0) {
2491
// Any of these characters implicitly begins the
2492
// next subpart if we are in the prefix
2493
if (subpart == 1) { // prefix subpart
2494
subpart = 0; // pattern proper subpart
2495
sub0Start = pos; // Reprocess this character
2499
// Fall through to append(ch)
2500
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
2501
// Any of these characters implicitly begins the
2502
// next subpart if we are in the prefix
2503
if (subpart == 1) { // prefix subpart
2504
subpart = 0; // pattern proper subpart
2505
sub0Start = pos; // Reprocess this character
2509
// Fall through to append(ch)
2510
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
2511
// Any of these characters implicitly begins the
2512
// next subpart if we are in the prefix
2513
if (subpart == 1) { // prefix subpart
2514
subpart = 0; // pattern proper subpart
2515
sub0Start = pos; // Reprocess this character
2518
pos += decimalSepLen;
2519
// Fall through to append(ch)
2520
} else if (ch >= zeroDigit && ch <= nineDigit) {
2521
// Any of these characters implicitly begins the
2522
// next subpart if we are in the prefix
2523
if (subpart == 1) { // prefix subpart
2524
subpart = 0; // pattern proper subpart
2525
sub0Start = pos; // Reprocess this character
2529
// Fall through to append(ch)
2530
} else if (ch == kCurrencySign) {
2531
// Use lookahead to determine if the currency sign is
2534
affix->append(kQuote); // Encode currency
2535
if (pos < pattern.length() && pattern[pos] == kCurrencySign)
2537
affix->append(kCurrencySign);
2538
++pos; // Skip over the doubled character
2541
// Fall through to append(ch)
2542
} else if (ch == kQuote) {
2543
// A quote outside quotes indicates either the opening
2544
// quote or two quotes, which is a quote literal. That is,
2545
// we have the first quote in 'do' or o''clock.
2547
if (pos < pattern.length() && pattern[pos] == kQuote) {
2548
affix->append(kQuote); // Encode quote
2550
// Fall through to append(ch)
2552
subpart += 2; // open quote
2555
} else if (pattern.compare(pos, separator.length(), separator) == 0) {
2556
// Don't allow separators in the prefix, and don't allow
2557
// separators in the second pattern (part == 1).
2558
if (subpart == 1 || part == 1) {
2559
// Unexpected separator
2560
debug("Unexpected separator")
2561
status = U_UNEXPECTED_TOKEN;
2562
syntaxError(pattern,pos,parseError);
2566
isPartDone = TRUE; // Go to next part
2567
pos += separator.length();
2569
} else if (pattern.compare(pos, percent.length(), percent) == 0) {
2570
// Next handle characters which are appended directly.
2571
if (multiplier != 1) {
2572
// Too many percent/perMill characters
2573
debug("Too many percent characters")
2574
status = U_MULTIPLE_PERCENT_SYMBOLS;
2575
syntaxError(pattern,pos,parseError);
2578
affix->append(kQuote); // Encode percent/perMill
2580
ch = kPatternPercent; // Use unlocalized pattern char
2581
pos += percent.length();
2582
// Fall through to append(ch)
2583
} else if (pattern.compare(pos, perMill.length(), perMill) == 0) {
2584
// Next handle characters which are appended directly.
2585
if (multiplier != 1) {
2586
// Too many percent/perMill characters
2587
debug("Too many perMill characters")
2588
status = U_MULTIPLE_PERMILL_SYMBOLS;
2589
syntaxError(pattern,pos,parseError);
2592
affix->append(kQuote); // Encode percent/perMill
2594
ch = kPatternPerMill; // Use unlocalized pattern char
2595
pos += perMill.length();
2596
// Fall through to append(ch)
2597
} else if (pattern.compare(pos, padEscape.length(), padEscape) == 0) {
2598
if (padPos >= 0 || // Multiple pad specifiers
2599
(pos+1) == pattern.length()) { // Nothing after padEscape
2600
debug("Multiple pad specifiers")
2601
status = U_MULTIPLE_PAD_SPECIFIERS;
2602
syntaxError(pattern,pos,parseError);
2606
padChar = pattern.char32At(++pos);
2607
pos += 1 + UTF_NEED_MULTIPLE_UCHAR(pattern.char32At(pos));
2608
// pos += padEscape.length();
2610
} else if (pattern.compare(pos, minus.length(), minus) == 0) {
2611
affix->append(kQuote); // Encode minus
2613
pos += minus.length();
2614
// Fall through to append(ch)
2615
} else if (pattern.compare(pos, plus.length(), plus) == 0) {
2616
affix->append(kQuote); // Encode plus
2618
pos += plus.length();
2619
// Fall through to append(ch)
2623
// Unquoted, non-special characters fall through to here, as
2624
// well as other code which needs to append something to the
2628
case 3: // Prefix subpart, in quote
2629
case 4: // Suffix subpart, in quote
2630
// A quote within quotes indicates either the closing
2631
// quote or two quotes, which is a quote literal. That is,
2632
// we have the second quote in 'do' or 'don''t'.
2635
if (pos < pattern.length() && pattern[pos] == kQuote) {
2637
affix->append(kQuote); // Encode quote
2638
// Fall through to append(ch)
2640
subpart -= 2; // close quote
2649
if (sub0Limit == 0) {
2650
sub0Limit = pattern.length();
2653
if (sub2Limit == 0) {
2654
sub2Limit = pattern.length();
2657
/* Handle patterns with no '0' pattern character. These patterns
2658
* are legal, but must be recodified to make sense. "##.###" ->
2659
* "#0.###". ".###" -> ".0##".
2661
* We allow patterns of the form "####" to produce a zeroDigitCount
2662
* of zero (got that?); although this seems like it might make it
2663
* possible for format() to produce empty strings, format() checks
2664
* for this condition and outputs a zero digit in this situation.
2665
* Having a zeroDigitCount of zero yields a minimum integer digits
2666
* of zero, which allows proper round-trip patterns. We don't want
2667
* "#" to become "#0" when toPattern() is called (even though that's
2668
* what it really is, semantically).
2670
if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
2671
// Handle "###.###" and "###." and ".###"
2674
++n; // Handle ".###"
2675
digitRightCount = digitLeftCount - n;
2676
digitLeftCount = n - 1;
2680
// Do syntax checking on the digits, decimal points, and quotes.
2681
if ((decimalPos < 0 && digitRightCount > 0) ||
2683
(decimalPos < digitLeftCount ||
2684
decimalPos > (digitLeftCount + zeroDigitCount))) ||
2685
groupingCount == 0 || groupingCount2 == 0 ||
2687
{ // subpart > 2 == unmatched quote
2688
debug("Syntax error")
2689
status = U_PATTERN_SYNTAX_ERROR;
2690
syntaxError(pattern,pos,parseError);
2694
// Make sure pad is at legal position before or after affix.
2696
if (padPos == start) {
2697
padPos = kPadBeforePrefix;
2698
} else if (padPos+2 == sub0Start) {
2699
padPos = kPadAfterPrefix;
2700
} else if (padPos == sub0Limit) {
2701
padPos = kPadBeforeSuffix;
2702
} else if (padPos+2 == sub2Limit) {
2703
padPos = kPadAfterSuffix;
2705
// Illegal pad position
2706
debug("Illegal pad position")
2707
status = U_ILLEGAL_PAD_POSITION;
2708
syntaxError(pattern,pos,parseError);
2714
delete fPosPrefixPattern;
2715
delete fPosSuffixPattern;
2716
delete fNegPrefixPattern;
2717
delete fNegSuffixPattern;
2718
fPosPrefixPattern = new UnicodeString(prefix);
2719
fPosSuffixPattern = new UnicodeString(suffix);
2720
fNegPrefixPattern = 0;
2721
fNegSuffixPattern = 0;
2723
fUseExponentialNotation = (expDigits >= 0);
2724
if (fUseExponentialNotation) {
2725
fMinExponentDigits = expDigits;
2727
fExponentSignAlwaysShown = expSignAlways;
2728
fIsCurrencyFormat = isCurrency;
2729
int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
2730
// The effectiveDecimalPos is the position the decimal is at or
2731
// would be at if there is no decimal. Note that if
2732
// decimalPos<0, then digitTotalCount == digitLeftCount +
2734
int effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
2735
setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
2736
setMaximumIntegerDigits(fUseExponentialNotation
2737
? digitLeftCount + getMinimumIntegerDigits()
2738
: kDoubleIntegerDigits);
2739
setMaximumFractionDigits(decimalPos >= 0
2740
? (digitTotalCount - decimalPos) : 0);
2741
setMinimumFractionDigits(decimalPos >= 0
2742
? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
2743
setGroupingUsed(groupingCount > 0);
2744
fGroupingSize = (groupingCount > 0) ? groupingCount : 0;
2745
fGroupingSize2 = (groupingCount2 > 0 && groupingCount2 != groupingCount)
2746
? groupingCount2 : 0;
2747
fMultiplier = multiplier;
2748
setDecimalSeparatorAlwaysShown(decimalPos == 0
2749
|| decimalPos == digitTotalCount);
2751
fPadPosition = (EPadPosition) padPos;
2752
// To compute the format width, first set up sub0Limit -
2753
// sub0Start. Add in prefix/suffix length later.
2755
// fFormatWidth = prefix.length() + suffix.length() +
2756
// sub0Limit - sub0Start;
2757
fFormatWidth = sub0Limit - sub0Start;
2762
if (roundingPos >= 0) {
2763
roundingInc.fDecimalAt = effectiveDecimalPos - roundingPos;
2764
if (fRoundingIncrement != NULL) {
2765
*fRoundingIncrement = roundingInc;
2767
fRoundingIncrement = new DigitList(roundingInc);
2769
fRoundingDouble = fRoundingIncrement->getDouble();
2770
fRoundingMode = kRoundHalfEven;
2772
setRoundingIncrement(0.0);
2775
fNegPrefixPattern = new UnicodeString(prefix);
2776
fNegSuffixPattern = new UnicodeString(suffix);
2780
if (pattern.length() == 0) {
2781
delete fNegPrefixPattern;
2782
delete fNegSuffixPattern;
2783
fNegPrefixPattern = NULL;
2784
fNegSuffixPattern = NULL;
2785
if (fPosPrefixPattern != NULL) {
2786
fPosPrefixPattern->remove();
2788
fPosPrefixPattern = new UnicodeString();
2790
if (fPosSuffixPattern != NULL) {
2791
fPosSuffixPattern->remove();
2793
fPosSuffixPattern = new UnicodeString();
2796
setMinimumIntegerDigits(0);
2797
setMaximumIntegerDigits(kDoubleIntegerDigits);
2798
setMinimumFractionDigits(0);
2799
setMaximumFractionDigits(kDoubleFractionDigits);
2801
fUseExponentialNotation = FALSE;
2802
fIsCurrencyFormat = FALSE;
2803
setGroupingUsed(FALSE);
2807
setDecimalSeparatorAlwaysShown(FALSE);
2809
setRoundingIncrement(0.0);
2812
// If there was no negative pattern, or if the negative pattern is
2813
// identical to the positive pattern, then prepend the minus sign to the
2814
// positive pattern to form the negative pattern.
2815
if (fNegPrefixPattern == NULL ||
2816
(*fNegPrefixPattern == *fPosPrefixPattern
2817
&& *fNegSuffixPattern == *fPosSuffixPattern)) {
2818
_copy_us_ptr(&fNegSuffixPattern, fPosSuffixPattern);
2819
if (fNegPrefixPattern == NULL) {
2820
fNegPrefixPattern = new UnicodeString();
2822
fNegPrefixPattern->remove();
2824
fNegPrefixPattern->append(kQuote).append(kPatternMinus)
2825
.append(*fPosPrefixPattern);
2829
s.append("\"").append(pattern).append("\"->");
2833
if (fFormatWidth > 0) {
2834
// Finish computing format width (see above)
2835
fFormatWidth += fPositivePrefix.length() + fPositiveSuffix.length();
2840
* Sets the maximum number of digits allowed in the integer portion of a
2841
* number. This override limits the integer digit count to 309.
2842
* @see NumberFormat#setMaximumIntegerDigits
2844
void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
2845
NumberFormat::setMaximumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
2849
* Sets the minimum number of digits allowed in the integer portion of a
2850
* number. This override limits the integer digit count to 309.
2851
* @see NumberFormat#setMinimumIntegerDigits
2853
void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
2854
NumberFormat::setMinimumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
2858
* Sets the maximum number of digits allowed in the fraction portion of a
2859
* number. This override limits the fraction digit count to 340.
2860
* @see NumberFormat#setMaximumFractionDigits
2862
void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
2863
NumberFormat::setMaximumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));
2867
* Sets the minimum number of digits allowed in the fraction portion of a
2868
* number. This override limits the fraction digit count to 340.
2869
* @see NumberFormat#setMinimumFractionDigits
2871
void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
2872
NumberFormat::setMinimumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));