- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- Search for |

- File List
- Globals

# decimfmt.h

Go to the documentation of this file.

    00001 /*
    00002 ********************************************************************************
    00003 *   Copyright (C) 1997-2006, International Business Machines
    00004 *   Corporation and others.  All Rights Reserved.
    00005 ********************************************************************************
    00006 *
    00007 * File DECIMFMT.H
    00008 *
    00009 * Modification History:
    00010 *
    00011 *   Date        Name        Description
    00012 *   02/19/97    aliu        Converted from java.
    00013 *   03/20/97    clhuang     Updated per C++ implementation.
    00014 *   04/03/97    aliu        Rewrote parsing and formatting completely, and
    00015 *                           cleaned up and debugged.  Actually works now.
    00016 *   04/17/97    aliu        Changed DigitCount to int per code review.
    00017 *   07/10/97    helena      Made ParsePosition a class and get rid of the function
    00018 *                           hiding problems.
    00019 *   09/09/97    aliu        Ported over support for exponential formats.
    00020 *    07/20/98    stephen        Changed documentation
    00021 ********************************************************************************
    00022 */
    00023
    00024 #ifndef DECIMFMT_H
    00025 #define DECIMFMT_H
    00026
    00027 #include "unicode/utypes.h"
    00033 #if !UCONFIG_NO_FORMATTING
    00034
    00035 #include "unicode/dcfmtsym.h"
    00036 #include "unicode/numfmt.h"
    00037 #include "unicode/locid.h"
    00038
    00039 U_NAMESPACE_BEGIN
    00040
    00041 class DigitList;
    00042 class ChoiceFormat;
    00043
    00603 class U_I18N_API DecimalFormat: public NumberFormat {
    00604 public:
    00609     enum ERoundingMode {
    00610         kRoundCeiling,
    00611         kRoundFloor,
    00612         kRoundDown,
    00613         kRoundUp,
    00614         kRoundHalfEven,
    00616         kRoundHalfDown,
    00618         kRoundHalfUp
    00620         // We don't support ROUND_UNNECESSARY
    00621     };
    00622
    00627     enum EPadPosition {
    00628         kPadBeforePrefix,
    00629         kPadAfterPrefix,
    00630         kPadBeforeSuffix,
    00631         kPadAfterSuffix
    00632     };
    00633
    00647     DecimalFormat(UErrorCode& status);
    00648
    00663     DecimalFormat(const UnicodeString& pattern,
    00664                   UErrorCode& status);
    00665
    00684     DecimalFormat(  const UnicodeString& pattern,
    00685                     DecimalFormatSymbols* symbolsToAdopt,
    00686                     UErrorCode& status);
    00687
    00707     DecimalFormat(  const UnicodeString& pattern,
    00708                     DecimalFormatSymbols* symbolsToAdopt,
    00709                     UParseError& parseError,
    00710                     UErrorCode& status);
    00728     DecimalFormat(  const UnicodeString& pattern,
    00729                     const DecimalFormatSymbols& symbols,
    00730                     UErrorCode& status);
    00731
    00738     DecimalFormat(const DecimalFormat& source);
    00739
    00746     DecimalFormat& operator=(const DecimalFormat& rhs);
    00747
    00752     virtual ~DecimalFormat();
    00753
    00761     virtual Format* clone(void) const;
    00762
    00771     virtual UBool operator==(const Format& other) const;
    00772
    00784     virtual UnicodeString& format(double number,
    00785                                   UnicodeString& appendTo,
    00786                                   FieldPosition& pos) const;
    00798     virtual UnicodeString& format(int32_t number,
    00799                                   UnicodeString& appendTo,
    00800                                   FieldPosition& pos) const;
    00812     virtual UnicodeString& format(int64_t number,
    00813                                   UnicodeString& appendTo,
    00814                                   FieldPosition& pos) const;
    00815
    00828     virtual UnicodeString& format(const Formattable& obj,
    00829                                   UnicodeString& appendTo,
    00830                                   FieldPosition& pos,
    00831                                   UErrorCode& status) const;
    00832
    00844     UnicodeString& format(const Formattable& obj,
    00845                           UnicodeString& appendTo,
    00846                           UErrorCode& status) const;
    00847
    00858     UnicodeString& format(double number,
    00859                           UnicodeString& appendTo) const;
    00860
    00872     UnicodeString& format(int32_t number,
    00873                           UnicodeString& appendTo) const;
    00874
    00886     UnicodeString& format(int64_t number,
    00887                           UnicodeString& appendTo) const;
    00907     virtual void parse(const UnicodeString& text,
    00908                        Formattable& result,
    00909                        ParsePosition& parsePosition) const;
    00910
    00911     // Declare here again to get rid of function hiding problems.
    00920     virtual void parse(const UnicodeString& text,
    00921                        Formattable& result,
    00922                        UErrorCode& status) const;
    00923
    00943     virtual Formattable& parseCurrency(const UnicodeString& text,
    00944                                        Formattable& result,
    00945                                        ParsePosition& pos) const;
    00946
    00954     virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
    00955
    00962     virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
    00963
    00970     virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
    00971
    00972
    00981     UnicodeString& getPositivePrefix(UnicodeString& result) const;
    00982
    00990     virtual void setPositivePrefix(const UnicodeString& newValue);
    00991
    01000     UnicodeString& getNegativePrefix(UnicodeString& result) const;
    01001
    01009     virtual void setNegativePrefix(const UnicodeString& newValue);
    01010
    01019     UnicodeString& getPositiveSuffix(UnicodeString& result) const;
    01020
    01028     virtual void setPositiveSuffix(const UnicodeString& newValue);
    01029
    01038     UnicodeString& getNegativeSuffix(UnicodeString& result) const;
    01039
    01047     virtual void setNegativeSuffix(const UnicodeString& newValue);
    01048
    01059     int32_t getMultiplier(void) const;
    01060
    01071     virtual void setMultiplier(int32_t newValue);
    01072
    01082     virtual double getRoundingIncrement(void) const;
    01083
    01094     virtual void setRoundingIncrement(double newValue);
    01095
    01104     virtual ERoundingMode getRoundingMode(void) const;
    01105
    01115     virtual void setRoundingMode(ERoundingMode roundingMode);
    01116
    01128     virtual int32_t getFormatWidth(void) const;
    01129
    01144     virtual void setFormatWidth(int32_t width);
    01145
    01158     virtual UnicodeString getPadCharacterString() const;
    01159
    01174     virtual void setPadCharacter(const UnicodeString &padChar);
    01175
    01194     virtual EPadPosition getPadPosition(void) const;
    01195
    01215     virtual void setPadPosition(EPadPosition padPos);
    01216
    01227     virtual UBool isScientificNotation(void);
    01228
    01244     virtual void setScientificNotation(UBool useScientific);
    01245
    01256     virtual int8_t getMinimumExponentDigits(void) const;
    01257
    01270     virtual void setMinimumExponentDigits(int8_t minExpDig);
    01271
    01284     virtual UBool isExponentSignAlwaysShown(void);
    01285
    01299     virtual void setExponentSignAlwaysShown(UBool expSignAlways);
    01300
    01312     int32_t getGroupingSize(void) const;
    01313
    01325     virtual void setGroupingSize(int32_t newValue);
    01326
    01345     int32_t getSecondaryGroupingSize(void) const;
    01346
    01358     virtual void setSecondaryGroupingSize(int32_t newValue);
    01359
    01368     UBool isDecimalSeparatorAlwaysShown(void) const;
    01369
    01378     virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
    01379
    01390     virtual UnicodeString& toPattern(UnicodeString& result) const;
    01391
    01402     virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
    01403
    01433     virtual void applyPattern(const UnicodeString& pattern,
    01434                              UParseError& parseError,
    01435                              UErrorCode& status);
    01444     virtual void applyPattern(const UnicodeString& pattern,
    01445                              UErrorCode& status);
    01446
    01477     virtual void applyLocalizedPattern(const UnicodeString& pattern,
    01478                                        UParseError& parseError,
    01479                                        UErrorCode& status);
    01480
    01490     virtual void applyLocalizedPattern(const UnicodeString& pattern,
    01491                                        UErrorCode& status);
    01492
    01493
    01503     virtual void setMaximumIntegerDigits(int32_t newValue);
    01504
    01514     virtual void setMinimumIntegerDigits(int32_t newValue);
    01515
    01525     virtual void setMaximumFractionDigits(int32_t newValue);
    01526
    01536     virtual void setMinimumFractionDigits(int32_t newValue);
    01537
    01545     int32_t getMinimumSignificantDigits() const;
    01546
    01554     int32_t getMaximumSignificantDigits() const;
    01555
    01565     void setMinimumSignificantDigits(int32_t min);
    01566
    01577     void setMaximumSignificantDigits(int32_t max);
    01578
    01585     UBool areSignificantDigitsUsed() const;
    01586
    01594     void setSignificantDigitsUsed(UBool useSignificantDigits);
    01595
    01596  public:
    01609     virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
    01610
    01616     virtual void setCurrency(const UChar* theCurrency);
    01617
    01623     static const char fgNumberPatterns[];
    01624
    01625 public:
    01626
    01638     static UClassID U_EXPORT2 getStaticClassID(void);
    01639
    01651     virtual UClassID getDynamicClassID(void) const;
    01652
    01653 private:
    01654     DecimalFormat(); // default constructor not implemented
    01655
    01656     int32_t precision(UBool isIntegral) const;
    01657
    01661     void construct(UErrorCode&               status,
    01662                    UParseError&             parseErr,
    01663                    const UnicodeString*     pattern = 0,
    01664                    DecimalFormatSymbols*    symbolsToAdopt = 0
    01665                    );
    01666
    01675     UnicodeString& toPattern(UnicodeString& result, UBool localized) const;
    01676
    01687     void applyPattern(const UnicodeString& pattern,
    01688                             UBool localized,
    01689                             UParseError& parseError,
    01690                             UErrorCode& status);
    01702     UnicodeString& subformat(UnicodeString& appendTo,
    01703                              FieldPosition& fieldPosition,
    01704                              DigitList& digits,
    01705                              UBool         isInteger) const;
    01706
    01707     void parse(const UnicodeString& text,
    01708                Formattable& result,
    01709                ParsePosition& pos,
    01710                UBool parseCurrency) const;
    01711
    01712     enum {
    01713         fgStatusInfinite,
    01714         fgStatusLength      // Leave last in list.
    01715     } StatusFlags;
    01716
    01717     UBool subparse(const UnicodeString& text, ParsePosition& parsePosition,
    01718                    DigitList& digits, UBool* status,
    01719                    UChar* currency) const;
    01720
    01721     int32_t skipPadding(const UnicodeString& text, int32_t position) const;
    01722
    01723     int32_t compareAffix(const UnicodeString& input,
    01724                          int32_t pos,
    01725                          UBool isNegative,
    01726                          UBool isPrefix,
    01727                          UChar* currency) const;
    01728
    01729     static int32_t compareSimpleAffix(const UnicodeString& affix,
    01730                                       const UnicodeString& input,
    01731                                       int32_t pos);
    01732
    01733     static int32_t skipRuleWhiteSpace(const UnicodeString& text, int32_t pos);
    01734
    01735     static int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos);
    01736
    01737     int32_t compareComplexAffix(const UnicodeString& affixPat,
    01738                                 const UnicodeString& input,
    01739                                 int32_t pos,
    01740                                 UChar* currency) const;
    01741
    01742     static int32_t match(const UnicodeString& text, int32_t pos, UChar32 ch);
    01743
    01744     static int32_t match(const UnicodeString& text, int32_t pos, const UnicodeString& str);
    01745
    01751     inline const UnicodeString &getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol) const;
    01752
    01753     int32_t appendAffix(UnicodeString& buf, double number,
    01754                         UBool isNegative, UBool isPrefix) const;
    01755
    01761     void appendAffixPattern(UnicodeString& appendTo, const UnicodeString& affix,
    01762                             UBool localized) const;
    01763
    01764     void appendAffixPattern(UnicodeString& appendTo,
    01765                             const UnicodeString* affixPattern,
    01766                             const UnicodeString& expAffix, UBool localized) const;
    01767
    01768     void expandAffix(const UnicodeString& pattern,
    01769                      UnicodeString& affix,
    01770                      double number,
    01771                      UBool doFormat) const;
    01772
    01773     void expandAffixes();
    01774
    01775     static double round(double a, ERoundingMode mode, UBool isNegative);
    01776
    01777     void addPadding(UnicodeString& appendTo,
    01778                     FieldPosition& fieldPosition,
    01779                     int32_t prefixLen, int32_t suffixLen) const;
    01780
    01781     UBool isGroupingPosition(int32_t pos) const;
    01782
    01783     void setCurrencyForSymbols();
    01784
    01788     //static const int8_t fgMaxDigit; // The largest digit, in this case 9
    01789
    01790     /*transient*/ //DigitList* fDigitList;
    01791
    01792     UnicodeString           fPositivePrefix;
    01793     UnicodeString           fPositiveSuffix;
    01794     UnicodeString           fNegativePrefix;
    01795     UnicodeString           fNegativeSuffix;
    01796     UnicodeString*          fPosPrefixPattern;
    01797     UnicodeString*          fPosSuffixPattern;
    01798     UnicodeString*          fNegPrefixPattern;
    01799     UnicodeString*          fNegSuffixPattern;
    01800
    01806     ChoiceFormat*           fCurrencyChoice;
    01807
    01808     int32_t                 fMultiplier;
    01809     int32_t                 fGroupingSize;
    01810     int32_t                 fGroupingSize2;
    01811     UBool                   fDecimalSeparatorAlwaysShown;
    01812     /*transient*/ UBool     fIsCurrencyFormat;
    01813     DecimalFormatSymbols*   fSymbols;
    01814
    01815     UBool                   fUseSignificantDigits;
    01816     int32_t                 fMinSignificantDigits;
    01817     int32_t                 fMaxSignificantDigits;
    01818
    01819     UBool                   fUseExponentialNotation;
    01820     int8_t                  fMinExponentDigits;
    01821     UBool                   fExponentSignAlwaysShown;
    01822
    01823     /* If fRoundingIncrement is NULL, there is no rounding.  Otherwise, round to
    01824      * fRoundingIncrement.getDouble().  Since this operation may be expensive,
    01825      * we cache the result in fRoundingDouble.  All methods that update
    01826      * fRoundingIncrement also update fRoundingDouble. */
    01827     DigitList*              fRoundingIncrement;
    01828     /*transient*/ double    fRoundingDouble;
    01829     ERoundingMode           fRoundingMode;
    01830
    01831     UChar32                 fPad;
    01832     int32_t                 fFormatWidth;
    01833     EPadPosition            fPadPosition;
    01834
    01835 protected:
    01836
    01845     virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
    01846
    01850     static const int32_t  kDoubleIntegerDigits;
    01854     static const int32_t  kDoubleFractionDigits;
    01855
    01866     static const int32_t  kMaxScientificIntegerDigits;
    01867 };
    01868
    01869 inline UnicodeString&
    01870 DecimalFormat::format(const Formattable& obj,
    01871                       UnicodeString& appendTo,
    01872                       UErrorCode& status) const {
    01873     // Don't use Format:: - use immediate base class only,
    01874     // in case immediate base modifies behavior later.
    01875     return NumberFormat::format(obj, appendTo, status);
    01876 }
    01877
    01878 inline UnicodeString&
    01879 DecimalFormat::format(double number,
    01880                       UnicodeString& appendTo) const {
    01881     FieldPosition pos(0);
    01882     return format(number, appendTo, pos);
    01883 }
    01884
    01885 inline UnicodeString&
    01886 DecimalFormat::format(int32_t number,
    01887                       UnicodeString& appendTo) const {
    01888     FieldPosition pos(0);
    01889     return format((int64_t)number, appendTo, pos);
    01890 }
    01891
    01892 inline const UnicodeString &
    01893 DecimalFormat::getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol) const {
    01894     return fSymbols->getConstSymbol(symbol);
    01895 }
    01896
    01897 U_NAMESPACE_END
    01898
    01899 #endif /* #if !UCONFIG_NO_FORMATTING */
    01900
    01901 #endif // _DECIMFMT
    01902 //eof