ICU 52.1  52.1
numfmt.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2013, 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"
40 
41 class NumberFormatTest;
42 
44 
45 #if !UCONFIG_NO_SERVICE
46 class NumberFormatFactory;
47 class StringEnumeration;
48 #endif
49 
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 
482  virtual UnicodeString& format(int64_t number,
483  UnicodeString& appendTo,
484  FieldPosition& pos,
485  UErrorCode& status) const;
500  virtual UnicodeString& format(int64_t number,
501  UnicodeString& appendTo,
502  FieldPositionIterator* posIter,
503  UErrorCode& status) const;
504 
521  virtual UnicodeString& format(const StringPiece &number,
522  UnicodeString& appendTo,
523  FieldPositionIterator* posIter,
524  UErrorCode& status) const;
525 public:
543  virtual UnicodeString& format(const DigitList &number,
544  UnicodeString& appendTo,
545  FieldPositionIterator* posIter,
546  UErrorCode& status) const;
547 
565  virtual UnicodeString& format(const DigitList &number,
566  UnicodeString& appendTo,
567  FieldPosition& pos,
568  UErrorCode& status) const;
569 
570 public:
571 
591  virtual void parse(const UnicodeString& text,
592  Formattable& result,
593  ParsePosition& parsePosition) const = 0;
594 
608  virtual void parse(const UnicodeString& text,
609  Formattable& result,
610  UErrorCode& status) const;
611 
631  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
632  ParsePosition& pos) const;
633 
645  UBool isParseIntegerOnly(void) const;
646 
654  virtual void setParseIntegerOnly(UBool value);
655 
663  virtual void setLenient(UBool enable);
664 
673  virtual UBool isLenient(void) const;
674 
683  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
684 
693  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
694  UErrorCode&);
695 
704  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
705  UNumberFormatStyle style,
706  UErrorCode& errorCode);
707 
712  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
713 
719  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
720  UErrorCode&);
721 
726  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
727 
733  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
734  UErrorCode&);
735 
740  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
741 
747  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
748  UErrorCode&);
749 
755  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
756 
757 #if !UCONFIG_NO_SERVICE
758 
765  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
766 
776  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
777 
784  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
785 #endif /* UCONFIG_NO_SERVICE */
786 
796  UBool isGroupingUsed(void) const;
797 
804  virtual void setGroupingUsed(UBool newValue);
805 
814  int32_t getMaximumIntegerDigits(void) const;
815 
828  virtual void setMaximumIntegerDigits(int32_t newValue);
829 
838  int32_t getMinimumIntegerDigits(void) const;
839 
850  virtual void setMinimumIntegerDigits(int32_t newValue);
851 
860  int32_t getMaximumFractionDigits(void) const;
861 
872  virtual void setMaximumFractionDigits(int32_t newValue);
873 
882  int32_t getMinimumFractionDigits(void) const;
883 
894  virtual void setMinimumFractionDigits(int32_t newValue);
895 
908  virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
909 
917  const UChar* getCurrency() const;
918 
919 public:
920 
929  static UClassID U_EXPORT2 getStaticClassID(void);
930 
942  virtual UClassID getDynamicClassID(void) const = 0;
943 
944 protected:
945 
950  NumberFormat();
951 
956  NumberFormat(const NumberFormat&);
957 
963 
972  virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
973 
974 #ifndef U_HIDE_INTERNAL_API
975 
981  static NumberFormat* makeInstance(const Locale& desiredLocale,
982  UNumberFormatStyle style,
983  UBool mustBeDecimalFormat,
984  UErrorCode& errorCode);
985 #endif /* U_HIDE_INTERNAL_API */
986 
987 private:
988 
989  static UBool isStyleSupported(UNumberFormatStyle style);
990 
998  static NumberFormat* makeInstance(const Locale& desiredLocale,
999  UNumberFormatStyle style,
1000  UErrorCode& errorCode);
1001 
1002  UBool fGroupingUsed;
1003  int32_t fMaxIntegerDigits;
1004  int32_t fMinIntegerDigits;
1005  int32_t fMaxFractionDigits;
1006  int32_t fMinFractionDigits;
1007 
1008  protected:
1009  static const int32_t gDefaultMaxIntegerDigits;
1010  static const int32_t gDefaultMinIntegerDigits;
1011 
1012  private:
1013  UBool fParseIntegerOnly;
1014  UBool fLenient; // TRUE => lenient parse is enabled
1015 
1016  // ISO currency code
1017  UChar fCurrency[4];
1018 
1019  friend class ICUNumberFormatFactory; // access to makeInstance
1020  friend class ICUNumberFormatService;
1021  friend class ::NumberFormatTest; // access to isStyleSupported()
1022 };
1023 
1024 #if !UCONFIG_NO_SERVICE
1025 
1034 public:
1035 
1040  virtual ~NumberFormatFactory();
1041 
1048  virtual UBool visible(void) const = 0;
1049 
1055  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1056 
1064  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1065 };
1066 
1072 protected:
1078 
1084 
1085 public:
1089  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1090 
1094  virtual ~SimpleNumberFormatFactory();
1095 
1099  virtual UBool visible(void) const;
1100 
1104  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1105 };
1106 #endif /* #if !UCONFIG_NO_SERVICE */
1107 
1108 // -------------------------------------
1109 
1110 inline UBool
1111 NumberFormat::isParseIntegerOnly() const
1112 {
1113  return fParseIntegerOnly;
1114 }
1115 
1116 inline UBool
1117 NumberFormat::isLenient() const
1118 {
1119  return fLenient;
1120 }
1121 
1123 
1124 #endif /* #if !UCONFIG_NO_FORMATTING */
1125 
1126 #endif // _NUMFMT
1127 //eof
Base class for all formats.
Definition: format.h:94
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1083
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1077
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:186
Abstract base class for all number formats.
Definition: numfmt.h:169
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.
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: 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:1071
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:137
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1033
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:204
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:278
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
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:200
C API: NumberFormat.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185