ICU 73.2  73.2
numfmt.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 ********************************************************************************
00005 * Copyright (C) 1997-2016, International Business Machines Corporation and others.
00006 * All Rights Reserved.
00007 ********************************************************************************
00008 *
00009 * File NUMFMT.H
00010 *
00011 * Modification History:
00012 *
00013 *   Date        Name        Description
00014 *   02/19/97    aliu        Converted from java.
00015 *   03/18/97    clhuang     Updated per C++ implementation.
00016 *   04/17/97    aliu        Changed DigitCount to int per code review.
00017 *    07/20/98    stephen        JDK 1.2 sync up. Added scientific support.
00018 *                            Changed naming conventions to match C++ guidelines
00019 *                            Deprecated Java style constants (eg, INTEGER_FIELD)
00020 ********************************************************************************
00021 */
00022 
00023 #ifndef NUMFMT_H
00024 #define NUMFMT_H
00025 
00026 
00027 #include "unicode/utypes.h"
00028 
00029 #if U_SHOW_CPLUSPLUS_API
00030 
00036 #if !UCONFIG_NO_FORMATTING
00037 
00038 #include "unicode/unistr.h"
00039 #include "unicode/format.h"
00040 #include "unicode/unum.h" // UNumberFormatStyle
00041 #include "unicode/locid.h"
00042 #include "unicode/stringpiece.h"
00043 #include "unicode/curramt.h"
00044 #include "unicode/udisplaycontext.h"
00045 
00046 class NumberFormatTest;
00047 
00048 U_NAMESPACE_BEGIN
00049 
00050 class SharedNumberFormat;
00051 
00052 #if !UCONFIG_NO_SERVICE
00053 class NumberFormatFactory;
00054 class StringEnumeration;
00055 #endif
00056 
00175 class U_I18N_API NumberFormat : public Format {
00176 public:
00186     enum ERoundingMode {
00187         kRoundCeiling,  
00188         kRoundFloor,    
00189         kRoundDown,     
00190         kRoundUp,       
00191         kRoundHalfEven, 
00193         kRoundHalfDown, 
00195         kRoundHalfUp,   
00201         kRoundUnnecessary,
00202 #ifndef U_HIDE_DRAFT_API
00203 
00207         kRoundHalfOdd,
00212         kRoundHalfCeiling,
00217         kRoundHalfFloor,
00218 #endif /* U_HIDE_DRAFT_API */
00219     };
00220 
00236     enum EAlignmentFields {
00238         kIntegerField = UNUM_INTEGER_FIELD,
00240         kFractionField = UNUM_FRACTION_FIELD,
00242         kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
00244         kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
00246         kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
00248         kExponentField = UNUM_EXPONENT_FIELD,
00250         kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
00252         kCurrencyField = UNUM_CURRENCY_FIELD,
00254         kPercentField = UNUM_PERCENT_FIELD,
00256         kPermillField = UNUM_PERMILL_FIELD,
00258         kSignField = UNUM_SIGN_FIELD,
00260         kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
00262         kCompactField = UNUM_COMPACT_FIELD,
00263 
00269         INTEGER_FIELD        = UNUM_INTEGER_FIELD,
00271         FRACTION_FIELD       = UNUM_FRACTION_FIELD
00272     };
00273 
00278     virtual ~NumberFormat();
00279 
00286     virtual NumberFormat* clone() const override = 0;
00287 
00294     virtual bool operator==(const Format& other) const override;
00295 
00296 
00297     using Format::format;
00298 
00314     virtual UnicodeString& format(const Formattable& obj,
00315                                   UnicodeString& appendTo,
00316                                   FieldPosition& pos,
00317                                   UErrorCode& status) const override;
00318 
00335     virtual UnicodeString& format(const Formattable& obj,
00336                                   UnicodeString& appendTo,
00337                                   FieldPositionIterator* posIter,
00338                                   UErrorCode& status) const override;
00339 
00368     virtual void parseObject(const UnicodeString& source,
00369                              Formattable& result,
00370                              ParsePosition& parse_pos) const override;
00371 
00382     UnicodeString& format(  double number,
00383                             UnicodeString& appendTo) const;
00384 
00395     UnicodeString& format(  int32_t number,
00396                             UnicodeString& appendTo) const;
00397 
00408     UnicodeString& format(  int64_t number,
00409                             UnicodeString& appendTo) const;
00410 
00423     virtual UnicodeString& format(double number,
00424                                   UnicodeString& appendTo,
00425                                   FieldPosition& pos) const = 0;
00440     virtual UnicodeString& format(double number,
00441                                   UnicodeString& appendTo,
00442                                   FieldPosition& pos,
00443                                   UErrorCode &status) const;
00458     virtual UnicodeString& format(double number,
00459                                   UnicodeString& appendTo,
00460                                   FieldPositionIterator* posIter,
00461                                   UErrorCode& status) const;
00474     virtual UnicodeString& format(int32_t number,
00475                                   UnicodeString& appendTo,
00476                                   FieldPosition& pos) const = 0;
00477 
00491     virtual UnicodeString& format(int32_t number,
00492                                   UnicodeString& appendTo,
00493                                   FieldPosition& pos,
00494                                   UErrorCode &status) const;
00495 
00510     virtual UnicodeString& format(int32_t number,
00511                                   UnicodeString& appendTo,
00512                                   FieldPositionIterator* posIter,
00513                                   UErrorCode& status) const;
00527     virtual UnicodeString& format(int64_t number,
00528                                   UnicodeString& appendTo,
00529                                   FieldPosition& pos) const;
00530 
00545     virtual UnicodeString& format(int64_t number,
00546                                   UnicodeString& appendTo,
00547                                   FieldPosition& pos,
00548                                   UErrorCode& status) const;
00563     virtual UnicodeString& format(int64_t number,
00564                                   UnicodeString& appendTo,
00565                                   FieldPositionIterator* posIter,
00566                                   UErrorCode& status) const;
00567 
00584     virtual UnicodeString& format(StringPiece number,
00585                                   UnicodeString& appendTo,
00586                                   FieldPositionIterator* posIter,
00587                                   UErrorCode& status) const;
00588 
00589 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
00590 
00608     virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
00609                                   UnicodeString& appendTo,
00610                                   FieldPositionIterator* posIter,
00611                                   UErrorCode& status) const;
00612 
00630     virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
00631                                   UnicodeString& appendTo,
00632                                   FieldPosition& pos,
00633                                   UErrorCode& status) const;
00634 
00654     virtual void parse(const UnicodeString& text,
00655                        Formattable& result,
00656                        ParsePosition& parsePosition) const = 0;
00657 
00673     virtual void parse(const UnicodeString& text,
00674                        Formattable& result,
00675                        UErrorCode& status) const;
00676 
00696     virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
00697                                           ParsePosition& pos) const;
00698 
00710     UBool isParseIntegerOnly(void) const;
00711 
00719     virtual void setParseIntegerOnly(UBool value);
00720 
00728     virtual void setLenient(UBool enable);
00729 
00738     virtual UBool isLenient(void) const;
00739 
00748     static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
00749 
00759     static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
00760                                         UErrorCode&);
00761 
00773     static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
00774                                                   UNumberFormatStyle style,
00775                                                   UErrorCode& errorCode);
00776 
00777 #ifndef U_HIDE_INTERNAL_API
00778 
00784     static NumberFormat* internalCreateInstance(
00785             const Locale& desiredLocale,
00786             UNumberFormatStyle style,
00787             UErrorCode& errorCode);
00788 
00796     static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
00797             const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
00798 
00799 #endif  /* U_HIDE_INTERNAL_API */
00800 
00808     static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
00809 
00818     static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
00819                                                 UErrorCode&);
00820 
00828     static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
00829 
00838     static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
00839                                                UErrorCode&);
00840 
00848     static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
00849 
00858     static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
00859                                                 UErrorCode&);
00860 
00866     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
00867 
00868 #if !UCONFIG_NO_SERVICE
00869 
00879     static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
00880 
00893     static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
00894 
00901     static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
00902 #endif /* UCONFIG_NO_SERVICE */
00903 
00913     UBool isGroupingUsed(void) const;
00914 
00921     virtual void setGroupingUsed(UBool newValue);
00922 
00931     int32_t getMaximumIntegerDigits(void) const;
00932 
00945     virtual void setMaximumIntegerDigits(int32_t newValue);
00946 
00955     int32_t getMinimumIntegerDigits(void) const;
00956 
00967     virtual void setMinimumIntegerDigits(int32_t newValue);
00968 
00977     int32_t getMaximumFractionDigits(void) const;
00978 
00989     virtual void setMaximumFractionDigits(int32_t newValue);
00990 
00999     int32_t getMinimumFractionDigits(void) const;
01000 
01011     virtual void setMinimumFractionDigits(int32_t newValue);
01012 
01025     virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
01026 
01034     const char16_t* getCurrency() const;
01035 
01045     virtual void setContext(UDisplayContext value, UErrorCode& status);
01046 
01057     virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
01058 
01065     virtual ERoundingMode getRoundingMode(void) const;
01066 
01072     virtual void setRoundingMode(ERoundingMode roundingMode);
01073 
01074 public:
01075 
01084     static UClassID U_EXPORT2 getStaticClassID(void);
01085 
01097     virtual UClassID getDynamicClassID(void) const override = 0;
01098 
01099 protected:
01100 
01105     NumberFormat();
01106 
01111     NumberFormat(const NumberFormat&);
01112 
01117     NumberFormat& operator=(const NumberFormat&);
01118 
01127     virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
01128 
01129 #ifndef U_HIDE_INTERNAL_API
01130 
01136     static NumberFormat* makeInstance(const Locale& desiredLocale,
01137                                       UNumberFormatStyle style,
01138                                       UBool mustBeDecimalFormat,
01139                                       UErrorCode& errorCode);
01140 #endif  /* U_HIDE_INTERNAL_API */
01141 
01142 private:
01143 
01144     static UBool isStyleSupported(UNumberFormatStyle style);
01145 
01153     static NumberFormat* makeInstance(const Locale& desiredLocale,
01154                                       UNumberFormatStyle style,
01155                                       UErrorCode& errorCode);
01156 
01157     UBool       fGroupingUsed;
01158     int32_t     fMaxIntegerDigits;
01159     int32_t     fMinIntegerDigits;
01160     int32_t     fMaxFractionDigits;
01161     int32_t     fMinFractionDigits;
01162 
01163   protected:
01165     static const int32_t gDefaultMaxIntegerDigits;
01167     static const int32_t gDefaultMinIntegerDigits;
01168 
01169   private:
01170     UBool      fParseIntegerOnly;
01171     UBool      fLenient; // true => lenient parse is enabled
01172 
01173     // ISO currency code
01174     char16_t      fCurrency[4];
01175 
01176     UDisplayContext fCapitalizationContext;
01177 
01178     friend class ICUNumberFormatFactory; // access to makeInstance
01179     friend class ICUNumberFormatService;
01180     friend class ::NumberFormatTest;  // access to isStyleSupported()
01181 };
01182 
01183 #if !UCONFIG_NO_SERVICE
01184 
01192 class U_I18N_API NumberFormatFactory : public UObject {
01193 public:
01194 
01199     virtual ~NumberFormatFactory();
01200 
01207     virtual UBool visible(void) const = 0;
01208 
01214     virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
01215 
01223     virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
01224 };
01225 
01230 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
01231 protected:
01236     const UBool _visible;
01237 
01242     UnicodeString _id;
01243 
01244 public:
01248     SimpleNumberFormatFactory(const Locale& locale, UBool visible = true);
01249 
01253     virtual ~SimpleNumberFormatFactory();
01254 
01258     virtual UBool visible(void) const override;
01259 
01263     virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const override;
01264 };
01265 #endif /* #if !UCONFIG_NO_SERVICE */
01266 
01267 // -------------------------------------
01268 
01269 inline UBool
01270 NumberFormat::isParseIntegerOnly() const
01271 {
01272     return fParseIntegerOnly;
01273 }
01274 
01275 inline UBool
01276 NumberFormat::isLenient() const
01277 {
01278     return fLenient;
01279 }
01280 
01281 U_NAMESPACE_END
01282 
01283 #endif /* #if !UCONFIG_NO_FORMATTING */
01284 
01285 #endif /* U_SHOW_CPLUSPLUS_API */
01286 
01287 #endif // _NUMFMT
01288 //eof