ICU 55.1  55.1
numfmt.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2015, International Business Machines Corporation and others.
4 * All Rights Reserved.
5 ********************************************************************************
6 *
7 * File NUMFMT.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/18/97 clhuang Updated per C++ implementation.
14 * 04/17/97 aliu Changed DigitCount to int per code review.
15 * 07/20/98 stephen JDK 1.2 sync up. Added scientific support.
16 * Changed naming conventions to match C++ guidelines
17 * Derecated Java style constants (eg, INTEGER_FIELD)
18 ********************************************************************************
19 */
20 
21 #ifndef NUMFMT_H
22 #define NUMFMT_H
23 
24 
25 #include "unicode/utypes.h"
26 
32 #if !UCONFIG_NO_FORMATTING
33 
34 #include "unicode/unistr.h"
35 #include "unicode/format.h"
36 #include "unicode/unum.h" // UNumberFormatStyle
37 #include "unicode/locid.h"
38 #include "unicode/stringpiece.h"
39 #include "unicode/curramt.h"
41 
42 class NumberFormatTest;
43 
45 
46 class SharedNumberFormat;
47 
48 #if !UCONFIG_NO_SERVICE
49 class NumberFormatFactory;
50 class StringEnumeration;
51 #endif
52 
173 public:
191  kIntegerField = UNUM_INTEGER_FIELD,
193  kFractionField = UNUM_FRACTION_FIELD,
195  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
197  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
199  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
201  kExponentField = UNUM_EXPONENT_FIELD,
203  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
205  kCurrencyField = UNUM_CURRENCY_FIELD,
207  kPercentField = UNUM_PERCENT_FIELD,
209  kPermillField = UNUM_PERMILL_FIELD,
211  kSignField = UNUM_SIGN_FIELD,
212 
218  INTEGER_FIELD = UNUM_INTEGER_FIELD,
220  FRACTION_FIELD = UNUM_FRACTION_FIELD
221  };
222 
227  virtual ~NumberFormat();
228 
235  virtual UBool operator==(const Format& other) const;
236 
237 
238  using Format::format;
239 
255  virtual UnicodeString& format(const Formattable& obj,
256  UnicodeString& appendTo,
257  FieldPosition& pos,
258  UErrorCode& status) const;
259 
276  virtual UnicodeString& format(const Formattable& obj,
277  UnicodeString& appendTo,
278  FieldPositionIterator* posIter,
279  UErrorCode& status) const;
280 
309  virtual void parseObject(const UnicodeString& source,
310  Formattable& result,
311  ParsePosition& parse_pos) const;
312 
323  UnicodeString& format( double number,
324  UnicodeString& appendTo) const;
325 
336  UnicodeString& format( int32_t number,
337  UnicodeString& appendTo) const;
338 
349  UnicodeString& format( int64_t number,
350  UnicodeString& appendTo) const;
351 
364  virtual UnicodeString& format(double number,
365  UnicodeString& appendTo,
366  FieldPosition& pos) const = 0;
381  virtual UnicodeString& format(double number,
382  UnicodeString& appendTo,
383  FieldPosition& pos,
384  UErrorCode &status) const;
399  virtual UnicodeString& format(double number,
400  UnicodeString& appendTo,
401  FieldPositionIterator* posIter,
402  UErrorCode& status) const;
415  virtual UnicodeString& format(int32_t number,
416  UnicodeString& appendTo,
417  FieldPosition& pos) const = 0;
418 
432  virtual UnicodeString& format(int32_t number,
433  UnicodeString& appendTo,
434  FieldPosition& pos,
435  UErrorCode &status) const;
436 
451  virtual UnicodeString& format(int32_t number,
452  UnicodeString& appendTo,
453  FieldPositionIterator* posIter,
454  UErrorCode& status) const;
468  virtual UnicodeString& format(int64_t number,
469  UnicodeString& appendTo,
470  FieldPosition& pos) const;
471 
485  virtual UnicodeString& format(int64_t number,
486  UnicodeString& appendTo,
487  FieldPosition& pos,
488  UErrorCode& status) const;
503  virtual UnicodeString& format(int64_t number,
504  UnicodeString& appendTo,
505  FieldPositionIterator* posIter,
506  UErrorCode& status) const;
507 
524  virtual UnicodeString& format(const StringPiece &number,
525  UnicodeString& appendTo,
526  FieldPositionIterator* posIter,
527  UErrorCode& status) const;
528 public:
546  virtual UnicodeString& format(const DigitList &number,
547  UnicodeString& appendTo,
548  FieldPositionIterator* posIter,
549  UErrorCode& status) const;
550 
568  virtual UnicodeString& format(const DigitList &number,
569  UnicodeString& appendTo,
570  FieldPosition& pos,
571  UErrorCode& status) const;
572 
573 public:
574 
594  virtual void parse(const UnicodeString& text,
595  Formattable& result,
596  ParsePosition& parsePosition) const = 0;
597 
611  virtual void parse(const UnicodeString& text,
612  Formattable& result,
613  UErrorCode& status) const;
614 
634  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
635  ParsePosition& pos) const;
636 
648  UBool isParseIntegerOnly(void) const;
649 
657  virtual void setParseIntegerOnly(UBool value);
658 
666  virtual void setLenient(UBool enable);
667 
676  virtual UBool isLenient(void) const;
677 
686  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
687 
696  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
697  UErrorCode&);
698 
707  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
708  UNumberFormatStyle style,
709  UErrorCode& errorCode);
710 
711 #ifndef U_HIDE_INTERNAL_API
712 
718  static NumberFormat* internalCreateInstance(
719  const Locale& desiredLocale,
720  UNumberFormatStyle style,
721  UErrorCode& errorCode);
722 
730  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
731  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
732 
733 #endif /* U_HIDE_INTERNAL_API */
734 
739  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
740 
746  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
747  UErrorCode&);
748 
753  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
754 
760  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
761  UErrorCode&);
762 
767  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
768 
774  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
775  UErrorCode&);
776 
782  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
783 
784 #if !UCONFIG_NO_SERVICE
785 
795  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
796 
809  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
810 
817  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
818 #endif /* UCONFIG_NO_SERVICE */
819 
829  UBool isGroupingUsed(void) const;
830 
837  virtual void setGroupingUsed(UBool newValue);
838 
847  int32_t getMaximumIntegerDigits(void) const;
848 
861  virtual void setMaximumIntegerDigits(int32_t newValue);
862 
871  int32_t getMinimumIntegerDigits(void) const;
872 
883  virtual void setMinimumIntegerDigits(int32_t newValue);
884 
893  int32_t getMaximumFractionDigits(void) const;
894 
905  virtual void setMaximumFractionDigits(int32_t newValue);
906 
915  int32_t getMinimumFractionDigits(void) const;
916 
927  virtual void setMinimumFractionDigits(int32_t newValue);
928 
941  virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
942 
950  const UChar* getCurrency() const;
951 
961  virtual void setContext(UDisplayContext value, UErrorCode& status);
962 
973  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
974 
975 public:
976 
985  static UClassID U_EXPORT2 getStaticClassID(void);
986 
998  virtual UClassID getDynamicClassID(void) const = 0;
999 
1000 protected:
1001 
1006  NumberFormat();
1007 
1012  NumberFormat(const NumberFormat&);
1013 
1019 
1028  virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
1029 
1030 #ifndef U_HIDE_INTERNAL_API
1031 
1037  static NumberFormat* makeInstance(const Locale& desiredLocale,
1038  UNumberFormatStyle style,
1039  UBool mustBeDecimalFormat,
1040  UErrorCode& errorCode);
1041 #endif /* U_HIDE_INTERNAL_API */
1042 
1043 private:
1044 
1045  static UBool isStyleSupported(UNumberFormatStyle style);
1046 
1054  static NumberFormat* makeInstance(const Locale& desiredLocale,
1055  UNumberFormatStyle style,
1056  UErrorCode& errorCode);
1057 
1058  UBool fGroupingUsed;
1059  int32_t fMaxIntegerDigits;
1060  int32_t fMinIntegerDigits;
1061  int32_t fMaxFractionDigits;
1062  int32_t fMinFractionDigits;
1063 
1064  protected:
1065  static const int32_t gDefaultMaxIntegerDigits;
1066  static const int32_t gDefaultMinIntegerDigits;
1067 
1068  private:
1069  UBool fParseIntegerOnly;
1070  UBool fLenient; // TRUE => lenient parse is enabled
1071 
1072  // ISO currency code
1073  UChar fCurrency[4];
1074 
1075  UDisplayContext fCapitalizationContext;
1076 
1077  friend class ICUNumberFormatFactory; // access to makeInstance
1078  friend class ICUNumberFormatService;
1079  friend class ::NumberFormatTest; // access to isStyleSupported()
1080 };
1081 
1082 #if !UCONFIG_NO_SERVICE
1083 
1092 public:
1093 
1098  virtual ~NumberFormatFactory();
1099 
1106  virtual UBool visible(void) const = 0;
1107 
1113  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1114 
1122  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1123 };
1124 
1130 protected:
1136 
1142 
1143 public:
1147  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1148 
1152  virtual ~SimpleNumberFormatFactory();
1153 
1157  virtual UBool visible(void) const;
1158 
1162  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1163 };
1164 #endif /* #if !UCONFIG_NO_SERVICE */
1165 
1166 // -------------------------------------
1167 
1168 inline UBool
1169 NumberFormat::isParseIntegerOnly() const
1170 {
1171  return fParseIntegerOnly;
1172 }
1173 
1174 inline UBool
1175 NumberFormat::isLenient() const
1176 {
1177  return fLenient;
1178 }
1179 
1181 
1182 #endif /* #if !UCONFIG_NO_FORMATTING */
1183 
1184 #endif // _NUMFMT
1185 //eof
Base class for all formats.
Definition: format.h:94
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1141
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1135
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:189
Abstract base class for all number formats.
Definition: numfmt.h:172
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
UDisplayContext
Display context settings.
C++ API: Unicode String.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:55
C API: Display context types (enum values)
C++ API: Base class for all formats.
virtual UBool visible(void) const =0
Return true if this factory will be visible.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
C++ API: StringPiece: Read-only byte string wrapper class.
C++ API: Currency Amount Object.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1129
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:139
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1091
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:53
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:238
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration...
Definition: umisc.h:55
uint16_t UChar
Define UChar to be UCHAR_TYPE, if that is #defined (for example, to char16_t), or wchar_t if that is ...
Definition: umachine.h:312
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
UDisplayContextType
Display context types, for getting values of a particular setting.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
C++ API: Locale ID object.
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
Basic definitions for ICU, for both C and C++ APIs.
Format & operator=(const Format &)
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:245
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:221
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:35
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
C API: NumberFormat.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185