ICU 59.1  59.1
numfmt.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines Corporation and others.
6 * All Rights Reserved.
7 ********************************************************************************
8 *
9 * File NUMFMT.H
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/18/97 clhuang Updated per C++ implementation.
16 * 04/17/97 aliu Changed DigitCount to int per code review.
17 * 07/20/98 stephen JDK 1.2 sync up. Added scientific support.
18 * Changed naming conventions to match C++ guidelines
19 * Derecated Java style constants (eg, INTEGER_FIELD)
20 ********************************************************************************
21 */
22 
23 #ifndef NUMFMT_H
24 #define NUMFMT_H
25 
26 
27 #include "unicode/utypes.h"
28 
34 #if !UCONFIG_NO_FORMATTING
35 
36 #include "unicode/unistr.h"
37 #include "unicode/format.h"
38 #include "unicode/unum.h" // UNumberFormatStyle
39 #include "unicode/locid.h"
40 #include "unicode/stringpiece.h"
41 #include "unicode/curramt.h"
43 
44 class NumberFormatTest;
45 
47 
48 class SharedNumberFormat;
49 
50 #if !UCONFIG_NO_SERVICE
51 class NumberFormatFactory;
52 class StringEnumeration;
53 #endif
54 
170 public:
188  kIntegerField = UNUM_INTEGER_FIELD,
190  kFractionField = UNUM_FRACTION_FIELD,
192  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
194  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
196  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
198  kExponentField = UNUM_EXPONENT_FIELD,
200  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
202  kCurrencyField = UNUM_CURRENCY_FIELD,
204  kPercentField = UNUM_PERCENT_FIELD,
206  kPermillField = UNUM_PERMILL_FIELD,
208  kSignField = UNUM_SIGN_FIELD,
209 
215  INTEGER_FIELD = UNUM_INTEGER_FIELD,
217  FRACTION_FIELD = UNUM_FRACTION_FIELD
218  };
219 
224  virtual ~NumberFormat();
225 
232  virtual UBool operator==(const Format& other) const;
233 
234 
235  using Format::format;
236 
252  virtual UnicodeString& format(const Formattable& obj,
253  UnicodeString& appendTo,
254  FieldPosition& pos,
255  UErrorCode& status) const;
256 
273  virtual UnicodeString& format(const Formattable& obj,
274  UnicodeString& appendTo,
275  FieldPositionIterator* posIter,
276  UErrorCode& status) const;
277 
306  virtual void parseObject(const UnicodeString& source,
307  Formattable& result,
308  ParsePosition& parse_pos) const;
309 
320  UnicodeString& format( double number,
321  UnicodeString& appendTo) const;
322 
333  UnicodeString& format( int32_t number,
334  UnicodeString& appendTo) const;
335 
346  UnicodeString& format( int64_t number,
347  UnicodeString& appendTo) const;
348 
361  virtual UnicodeString& format(double number,
362  UnicodeString& appendTo,
363  FieldPosition& pos) const = 0;
378  virtual UnicodeString& format(double number,
379  UnicodeString& appendTo,
380  FieldPosition& pos,
381  UErrorCode &status) const;
396  virtual UnicodeString& format(double number,
397  UnicodeString& appendTo,
398  FieldPositionIterator* posIter,
399  UErrorCode& status) const;
412  virtual UnicodeString& format(int32_t number,
413  UnicodeString& appendTo,
414  FieldPosition& pos) const = 0;
415 
429  virtual UnicodeString& format(int32_t number,
430  UnicodeString& appendTo,
431  FieldPosition& pos,
432  UErrorCode &status) const;
433 
448  virtual UnicodeString& format(int32_t number,
449  UnicodeString& appendTo,
450  FieldPositionIterator* posIter,
451  UErrorCode& status) const;
465  virtual UnicodeString& format(int64_t number,
466  UnicodeString& appendTo,
467  FieldPosition& pos) const;
468 
483  virtual UnicodeString& format(int64_t number,
484  UnicodeString& appendTo,
485  FieldPosition& pos,
486  UErrorCode& status) const;
501  virtual UnicodeString& format(int64_t number,
502  UnicodeString& appendTo,
503  FieldPositionIterator* posIter,
504  UErrorCode& status) const;
505 
522  virtual UnicodeString& format(StringPiece number,
523  UnicodeString& appendTo,
524  FieldPositionIterator* posIter,
525  UErrorCode& status) const;
526 public:
544  virtual UnicodeString& format(const DigitList &number,
545  UnicodeString& appendTo,
546  FieldPositionIterator* posIter,
547  UErrorCode& status) const;
548 
566  virtual UnicodeString& format(const DigitList &number,
567  UnicodeString& appendTo,
568  FieldPosition& pos,
569  UErrorCode& status) const;
570 
571 public:
572 
592  virtual void parse(const UnicodeString& text,
593  Formattable& result,
594  ParsePosition& parsePosition) const = 0;
595 
609  virtual void parse(const UnicodeString& text,
610  Formattable& result,
611  UErrorCode& status) const;
612 
632  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
633  ParsePosition& pos) const;
634 
646  UBool isParseIntegerOnly(void) const;
647 
655  virtual void setParseIntegerOnly(UBool value);
656 
664  virtual void setLenient(UBool enable);
665 
674  virtual UBool isLenient(void) const;
675 
681  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
682 
689  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
690  UErrorCode&);
691 
700  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
701  UNumberFormatStyle style,
702  UErrorCode& errorCode);
703 
704 #ifndef U_HIDE_INTERNAL_API
705 
711  static NumberFormat* internalCreateInstance(
712  const Locale& desiredLocale,
713  UNumberFormatStyle style,
714  UErrorCode& errorCode);
715 
723  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
724  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
725 
726 #endif /* U_HIDE_INTERNAL_API */
727 
732  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
733 
739  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
740  UErrorCode&);
741 
746  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
747 
753  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
754  UErrorCode&);
755 
760  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
761 
767  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
768  UErrorCode&);
769 
775  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
776 
777 #if !UCONFIG_NO_SERVICE
778 
788  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
789 
802  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
803 
810  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
811 #endif /* UCONFIG_NO_SERVICE */
812 
822  UBool isGroupingUsed(void) const;
823 
830  virtual void setGroupingUsed(UBool newValue);
831 
840  int32_t getMaximumIntegerDigits(void) const;
841 
854  virtual void setMaximumIntegerDigits(int32_t newValue);
855 
864  int32_t getMinimumIntegerDigits(void) const;
865 
876  virtual void setMinimumIntegerDigits(int32_t newValue);
877 
886  int32_t getMaximumFractionDigits(void) const;
887 
898  virtual void setMaximumFractionDigits(int32_t newValue);
899 
908  int32_t getMinimumFractionDigits(void) const;
909 
920  virtual void setMinimumFractionDigits(int32_t newValue);
921 
934  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
935 
943  const char16_t* getCurrency() const;
944 
954  virtual void setContext(UDisplayContext value, UErrorCode& status);
955 
966  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
967 
968 public:
969 
978  static UClassID U_EXPORT2 getStaticClassID(void);
979 
991  virtual UClassID getDynamicClassID(void) const = 0;
992 
993 protected:
994 
999  NumberFormat();
1000 
1005  NumberFormat(const NumberFormat&);
1006 
1012 
1021  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1022 
1023 #ifndef U_HIDE_INTERNAL_API
1024 
1030  static NumberFormat* makeInstance(const Locale& desiredLocale,
1031  UNumberFormatStyle style,
1032  UBool mustBeDecimalFormat,
1033  UErrorCode& errorCode);
1034 #endif /* U_HIDE_INTERNAL_API */
1035 
1036 private:
1037 
1038  static UBool isStyleSupported(UNumberFormatStyle style);
1039 
1047  static NumberFormat* makeInstance(const Locale& desiredLocale,
1048  UNumberFormatStyle style,
1049  UErrorCode& errorCode);
1050 
1051  UBool fGroupingUsed;
1052  int32_t fMaxIntegerDigits;
1053  int32_t fMinIntegerDigits;
1054  int32_t fMaxFractionDigits;
1055  int32_t fMinFractionDigits;
1056 
1057  protected:
1059  static const int32_t gDefaultMaxIntegerDigits;
1061  static const int32_t gDefaultMinIntegerDigits;
1062 
1063  private:
1064  UBool fParseIntegerOnly;
1065  UBool fLenient; // TRUE => lenient parse is enabled
1066 
1067  // ISO currency code
1068  char16_t fCurrency[4];
1069 
1070  UDisplayContext fCapitalizationContext;
1071 
1072  friend class ICUNumberFormatFactory; // access to makeInstance
1073  friend class ICUNumberFormatService;
1074  friend class ::NumberFormatTest; // access to isStyleSupported()
1075 };
1076 
1077 #if !UCONFIG_NO_SERVICE
1078 
1087 public:
1088 
1093  virtual ~NumberFormatFactory();
1094 
1101  virtual UBool visible(void) const = 0;
1102 
1108  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1109 
1117  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1118 };
1119 
1125 protected:
1131 
1137 
1138 public:
1142  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1143 
1147  virtual ~SimpleNumberFormatFactory();
1148 
1152  virtual UBool visible(void) const;
1153 
1157  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1158 };
1159 #endif /* #if !UCONFIG_NO_SERVICE */
1160 
1161 // -------------------------------------
1162 
1163 inline UBool
1165 {
1166  return fParseIntegerOnly;
1167 }
1168 
1169 inline UBool
1171 {
1172  return fLenient;
1173 }
1174 
1176 
1177 #endif /* #if !UCONFIG_NO_FORMATTING */
1178 
1179 #endif // _NUMFMT
1180 //eof
Base class for all formats.
Definition: format.h:96
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1136
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1130
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:186
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
static const int32_t gDefaultMinIntegerDigits
Definition: numfmt.h:1061
Abstract base class for all number formats.
Definition: numfmt.h:169
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:93
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:57
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.
UBool isParseIntegerOnly(void) const
Return true if this format will parse numbers as integers only.
Definition: numfmt.h:1164
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
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:131
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1124
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:142
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1086
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1059
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:240
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration...
Definition: umisc.h:57
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
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:396
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
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:296
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:68
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1170
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:37
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
C API: NumberFormat.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188