ICU 63.1  63.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 
174 public:
189  kRoundHalfEven,
191  kRoundHalfDown,
193  kRoundHalfUp,
199  kRoundUnnecessary
200  };
201 
219  kIntegerField = UNUM_INTEGER_FIELD,
221  kFractionField = UNUM_FRACTION_FIELD,
223  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
225  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
227  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
229  kExponentField = UNUM_EXPONENT_FIELD,
231  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
233  kCurrencyField = UNUM_CURRENCY_FIELD,
235  kPercentField = UNUM_PERCENT_FIELD,
237  kPermillField = UNUM_PERMILL_FIELD,
239  kSignField = UNUM_SIGN_FIELD,
240 
246  INTEGER_FIELD = UNUM_INTEGER_FIELD,
248  FRACTION_FIELD = UNUM_FRACTION_FIELD
249  };
250 
255  virtual ~NumberFormat();
256 
263  virtual UBool operator==(const Format& other) const;
264 
265 
266  using Format::format;
267 
283  virtual UnicodeString& format(const Formattable& obj,
284  UnicodeString& appendTo,
285  FieldPosition& pos,
286  UErrorCode& status) const;
287 
304  virtual UnicodeString& format(const Formattable& obj,
305  UnicodeString& appendTo,
306  FieldPositionIterator* posIter,
307  UErrorCode& status) const;
308 
337  virtual void parseObject(const UnicodeString& source,
338  Formattable& result,
339  ParsePosition& parse_pos) const;
340 
351  UnicodeString& format( double number,
352  UnicodeString& appendTo) const;
353 
364  UnicodeString& format( int32_t number,
365  UnicodeString& appendTo) const;
366 
377  UnicodeString& format( int64_t number,
378  UnicodeString& appendTo) const;
379 
392  virtual UnicodeString& format(double number,
393  UnicodeString& appendTo,
394  FieldPosition& pos) const = 0;
409  virtual UnicodeString& format(double number,
410  UnicodeString& appendTo,
411  FieldPosition& pos,
412  UErrorCode &status) const;
427  virtual UnicodeString& format(double number,
428  UnicodeString& appendTo,
429  FieldPositionIterator* posIter,
430  UErrorCode& status) const;
443  virtual UnicodeString& format(int32_t number,
444  UnicodeString& appendTo,
445  FieldPosition& pos) const = 0;
446 
460  virtual UnicodeString& format(int32_t number,
461  UnicodeString& appendTo,
462  FieldPosition& pos,
463  UErrorCode &status) const;
464 
479  virtual UnicodeString& format(int32_t number,
480  UnicodeString& appendTo,
481  FieldPositionIterator* posIter,
482  UErrorCode& status) const;
496  virtual UnicodeString& format(int64_t number,
497  UnicodeString& appendTo,
498  FieldPosition& pos) const;
499 
514  virtual UnicodeString& format(int64_t number,
515  UnicodeString& appendTo,
516  FieldPosition& pos,
517  UErrorCode& status) const;
532  virtual UnicodeString& format(int64_t number,
533  UnicodeString& appendTo,
534  FieldPositionIterator* posIter,
535  UErrorCode& status) const;
536 
553  virtual UnicodeString& format(StringPiece number,
554  UnicodeString& appendTo,
555  FieldPositionIterator* posIter,
556  UErrorCode& status) const;
557 
558 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
559 
577  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
578  UnicodeString& appendTo,
579  FieldPositionIterator* posIter,
580  UErrorCode& status) const;
581 
599  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
600  UnicodeString& appendTo,
601  FieldPosition& pos,
602  UErrorCode& status) const;
603 
623  virtual void parse(const UnicodeString& text,
624  Formattable& result,
625  ParsePosition& parsePosition) const = 0;
626 
640  virtual void parse(const UnicodeString& text,
641  Formattable& result,
642  UErrorCode& status) const;
643 
663  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
664  ParsePosition& pos) const;
665 
677  UBool isParseIntegerOnly(void) const;
678 
686  virtual void setParseIntegerOnly(UBool value);
687 
695  virtual void setLenient(UBool enable);
696 
705  virtual UBool isLenient(void) const;
706 
715  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
716 
726  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
727  UErrorCode&);
728 
740  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
741  UNumberFormatStyle style,
742  UErrorCode& errorCode);
743 
744 #ifndef U_HIDE_INTERNAL_API
745 
751  static NumberFormat* internalCreateInstance(
752  const Locale& desiredLocale,
753  UNumberFormatStyle style,
754  UErrorCode& errorCode);
755 
763  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
764  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
765 
766 #endif /* U_HIDE_INTERNAL_API */
767 
775  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
776 
785  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
786  UErrorCode&);
787 
795  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
796 
805  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
806  UErrorCode&);
807 
815  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
816 
825  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
826  UErrorCode&);
827 
833  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
834 
835 #if !UCONFIG_NO_SERVICE
836 
846  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
847 
860  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
861 
868  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
869 #endif /* UCONFIG_NO_SERVICE */
870 
880  UBool isGroupingUsed(void) const;
881 
888  virtual void setGroupingUsed(UBool newValue);
889 
898  int32_t getMaximumIntegerDigits(void) const;
899 
912  virtual void setMaximumIntegerDigits(int32_t newValue);
913 
922  int32_t getMinimumIntegerDigits(void) const;
923 
934  virtual void setMinimumIntegerDigits(int32_t newValue);
935 
944  int32_t getMaximumFractionDigits(void) const;
945 
956  virtual void setMaximumFractionDigits(int32_t newValue);
957 
966  int32_t getMinimumFractionDigits(void) const;
967 
978  virtual void setMinimumFractionDigits(int32_t newValue);
979 
992  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
993 
1001  const char16_t* getCurrency() const;
1002 
1012  virtual void setContext(UDisplayContext value, UErrorCode& status);
1013 
1024  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1025 
1032  virtual ERoundingMode getRoundingMode(void) const;
1033 
1039  virtual void setRoundingMode(ERoundingMode roundingMode);
1040 
1041 public:
1042 
1051  static UClassID U_EXPORT2 getStaticClassID(void);
1052 
1064  virtual UClassID getDynamicClassID(void) const = 0;
1065 
1066 protected:
1067 
1072  NumberFormat();
1073 
1078  NumberFormat(const NumberFormat&);
1079 
1085 
1094  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1095 
1096 #ifndef U_HIDE_INTERNAL_API
1097 
1103  static NumberFormat* makeInstance(const Locale& desiredLocale,
1104  UNumberFormatStyle style,
1105  UBool mustBeDecimalFormat,
1106  UErrorCode& errorCode);
1107 #endif /* U_HIDE_INTERNAL_API */
1108 
1109 private:
1110 
1111  static UBool isStyleSupported(UNumberFormatStyle style);
1112 
1120  static NumberFormat* makeInstance(const Locale& desiredLocale,
1121  UNumberFormatStyle style,
1122  UErrorCode& errorCode);
1123 
1124  UBool fGroupingUsed;
1125  int32_t fMaxIntegerDigits;
1126  int32_t fMinIntegerDigits;
1127  int32_t fMaxFractionDigits;
1128  int32_t fMinFractionDigits;
1129 
1130  protected:
1132  static const int32_t gDefaultMaxIntegerDigits;
1134  static const int32_t gDefaultMinIntegerDigits;
1135 
1136  private:
1137  UBool fParseIntegerOnly;
1138  UBool fLenient; // TRUE => lenient parse is enabled
1139 
1140  // ISO currency code
1141  char16_t fCurrency[4];
1142 
1143  UDisplayContext fCapitalizationContext;
1144 
1145  friend class ICUNumberFormatFactory; // access to makeInstance
1146  friend class ICUNumberFormatService;
1147  friend class ::NumberFormatTest; // access to isStyleSupported()
1148 };
1149 
1150 #if !UCONFIG_NO_SERVICE
1151 
1160 public:
1161 
1166  virtual ~NumberFormatFactory();
1167 
1174  virtual UBool visible(void) const = 0;
1175 
1181  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1182 
1190  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1191 };
1192 
1198 protected:
1204 
1210 
1211 public:
1215  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1216 
1220  virtual ~SimpleNumberFormatFactory();
1221 
1225  virtual UBool visible(void) const;
1226 
1230  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1231 };
1232 #endif /* #if !UCONFIG_NO_SERVICE */
1233 
1234 // -------------------------------------
1235 
1236 inline UBool
1238 {
1239  return fParseIntegerOnly;
1240 }
1241 
1242 inline UBool
1244 {
1245  return fLenient;
1246 }
1247 
1249 
1250 #endif /* #if !UCONFIG_NO_FORMATTING */
1251 
1252 #endif // _NUMFMT
1253 //eof
Base class for all formats.
Definition: format.h:96
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1209
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1203
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:217
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:1134
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
UDisplayContext
Display context settings.
Round towards zero.
Definition: numfmt.h:187
C++ API: Unicode String.
Round towards positive infinity.
Definition: numfmt.h:185
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:90
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:1237
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
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:137
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1197
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1159
Round towards negative infinity.
Definition: numfmt.h:186
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:1132
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Round away from zero.
Definition: numfmt.h:188
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
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:138
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:401
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
ERoundingMode
Rounding mode.
Definition: numfmt.h:184
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:289
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
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:220
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1243
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:225
C API: Compatibility APIs for number formatting.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192