ICU 62.1  62.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 
175 public:
190  kRoundHalfEven,
192  kRoundHalfDown,
194  kRoundHalfUp,
200  kRoundUnnecessary
201  };
202 
220  kIntegerField = UNUM_INTEGER_FIELD,
222  kFractionField = UNUM_FRACTION_FIELD,
224  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
226  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
228  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
230  kExponentField = UNUM_EXPONENT_FIELD,
232  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
234  kCurrencyField = UNUM_CURRENCY_FIELD,
236  kPercentField = UNUM_PERCENT_FIELD,
238  kPermillField = UNUM_PERMILL_FIELD,
240  kSignField = UNUM_SIGN_FIELD,
241 
247  INTEGER_FIELD = UNUM_INTEGER_FIELD,
249  FRACTION_FIELD = UNUM_FRACTION_FIELD
250  };
251 
256  virtual ~NumberFormat();
257 
264  virtual UBool operator==(const Format& other) const;
265 
266 
267  using Format::format;
268 
284  virtual UnicodeString& format(const Formattable& obj,
285  UnicodeString& appendTo,
286  FieldPosition& pos,
287  UErrorCode& status) const;
288 
305  virtual UnicodeString& format(const Formattable& obj,
306  UnicodeString& appendTo,
307  FieldPositionIterator* posIter,
308  UErrorCode& status) const;
309 
338  virtual void parseObject(const UnicodeString& source,
339  Formattable& result,
340  ParsePosition& parse_pos) const;
341 
352  UnicodeString& format( double number,
353  UnicodeString& appendTo) const;
354 
365  UnicodeString& format( int32_t number,
366  UnicodeString& appendTo) const;
367 
378  UnicodeString& format( int64_t number,
379  UnicodeString& appendTo) const;
380 
393  virtual UnicodeString& format(double number,
394  UnicodeString& appendTo,
395  FieldPosition& pos) const = 0;
410  virtual UnicodeString& format(double number,
411  UnicodeString& appendTo,
412  FieldPosition& pos,
413  UErrorCode &status) const;
428  virtual UnicodeString& format(double number,
429  UnicodeString& appendTo,
430  FieldPositionIterator* posIter,
431  UErrorCode& status) const;
444  virtual UnicodeString& format(int32_t number,
445  UnicodeString& appendTo,
446  FieldPosition& pos) const = 0;
447 
461  virtual UnicodeString& format(int32_t number,
462  UnicodeString& appendTo,
463  FieldPosition& pos,
464  UErrorCode &status) const;
465 
480  virtual UnicodeString& format(int32_t number,
481  UnicodeString& appendTo,
482  FieldPositionIterator* posIter,
483  UErrorCode& status) const;
497  virtual UnicodeString& format(int64_t number,
498  UnicodeString& appendTo,
499  FieldPosition& pos) const;
500 
515  virtual UnicodeString& format(int64_t number,
516  UnicodeString& appendTo,
517  FieldPosition& pos,
518  UErrorCode& status) const;
533  virtual UnicodeString& format(int64_t number,
534  UnicodeString& appendTo,
535  FieldPositionIterator* posIter,
536  UErrorCode& status) const;
537 
554  virtual UnicodeString& format(StringPiece number,
555  UnicodeString& appendTo,
556  FieldPositionIterator* posIter,
557  UErrorCode& status) const;
558 
559 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
560 
578  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
579  UnicodeString& appendTo,
580  FieldPositionIterator* posIter,
581  UErrorCode& status) const;
582 
600  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
601  UnicodeString& appendTo,
602  FieldPosition& pos,
603  UErrorCode& status) const;
604 
624  virtual void parse(const UnicodeString& text,
625  Formattable& result,
626  ParsePosition& parsePosition) const = 0;
627 
641  virtual void parse(const UnicodeString& text,
642  Formattable& result,
643  UErrorCode& status) const;
644 
664  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
665  ParsePosition& pos) const;
666 
678  UBool isParseIntegerOnly(void) const;
679 
687  virtual void setParseIntegerOnly(UBool value);
688 
696  virtual void setLenient(UBool enable);
697 
706  virtual UBool isLenient(void) const;
707 
716  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
717 
727  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
728  UErrorCode&);
729 
741  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
742  UNumberFormatStyle style,
743  UErrorCode& errorCode);
744 
745 #ifndef U_HIDE_INTERNAL_API
746 
752  static NumberFormat* internalCreateInstance(
753  const Locale& desiredLocale,
754  UNumberFormatStyle style,
755  UErrorCode& errorCode);
756 
764  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
765  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
766 
767 #endif /* U_HIDE_INTERNAL_API */
768 
776  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
777 
786  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
787  UErrorCode&);
788 
796  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
797 
806  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
807  UErrorCode&);
808 
816  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
817 
826  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
827  UErrorCode&);
828 
834  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
835 
836 #if !UCONFIG_NO_SERVICE
837 
847  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
848 
861  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
862 
869  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
870 #endif /* UCONFIG_NO_SERVICE */
871 
881  UBool isGroupingUsed(void) const;
882 
889  virtual void setGroupingUsed(UBool newValue);
890 
899  int32_t getMaximumIntegerDigits(void) const;
900 
913  virtual void setMaximumIntegerDigits(int32_t newValue);
914 
923  int32_t getMinimumIntegerDigits(void) const;
924 
935  virtual void setMinimumIntegerDigits(int32_t newValue);
936 
945  int32_t getMaximumFractionDigits(void) const;
946 
957  virtual void setMaximumFractionDigits(int32_t newValue);
958 
967  int32_t getMinimumFractionDigits(void) const;
968 
979  virtual void setMinimumFractionDigits(int32_t newValue);
980 
993  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
994 
1002  const char16_t* getCurrency() const;
1003 
1013  virtual void setContext(UDisplayContext value, UErrorCode& status);
1014 
1025  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1026 
1033  virtual ERoundingMode getRoundingMode(void) const;
1034 
1040  virtual void setRoundingMode(ERoundingMode roundingMode);
1041 
1042 public:
1043 
1052  static UClassID U_EXPORT2 getStaticClassID(void);
1053 
1065  virtual UClassID getDynamicClassID(void) const = 0;
1066 
1067 protected:
1068 
1073  NumberFormat();
1074 
1079  NumberFormat(const NumberFormat&);
1080 
1086 
1095  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1096 
1097 #ifndef U_HIDE_INTERNAL_API
1098 
1104  static NumberFormat* makeInstance(const Locale& desiredLocale,
1105  UNumberFormatStyle style,
1106  UBool mustBeDecimalFormat,
1107  UErrorCode& errorCode);
1108 #endif /* U_HIDE_INTERNAL_API */
1109 
1110 private:
1111 
1112  static UBool isStyleSupported(UNumberFormatStyle style);
1113 
1121  static NumberFormat* makeInstance(const Locale& desiredLocale,
1122  UNumberFormatStyle style,
1123  UErrorCode& errorCode);
1124 
1125  UBool fGroupingUsed;
1126  int32_t fMaxIntegerDigits;
1127  int32_t fMinIntegerDigits;
1128  int32_t fMaxFractionDigits;
1129  int32_t fMinFractionDigits;
1130 
1131  protected:
1133  static const int32_t gDefaultMaxIntegerDigits;
1135  static const int32_t gDefaultMinIntegerDigits;
1136 
1137  private:
1138  UBool fParseIntegerOnly;
1139  UBool fLenient; // TRUE => lenient parse is enabled
1140 
1141  // ISO currency code
1142  char16_t fCurrency[4];
1143 
1144  UDisplayContext fCapitalizationContext;
1145 
1146  friend class ICUNumberFormatFactory; // access to makeInstance
1147  friend class ICUNumberFormatService;
1148  friend class ::NumberFormatTest; // access to isStyleSupported()
1149 };
1150 
1151 #if !UCONFIG_NO_SERVICE
1152 
1161 public:
1162 
1167  virtual ~NumberFormatFactory();
1168 
1175  virtual UBool visible(void) const = 0;
1176 
1182  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1183 
1191  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1192 };
1193 
1199 protected:
1205 
1211 
1212 public:
1216  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1217 
1221  virtual ~SimpleNumberFormatFactory();
1222 
1226  virtual UBool visible(void) const;
1227 
1231  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1232 };
1233 #endif /* #if !UCONFIG_NO_SERVICE */
1234 
1235 // -------------------------------------
1236 
1237 inline UBool
1239 {
1240  return fParseIntegerOnly;
1241 }
1242 
1243 inline UBool
1245 {
1246  return fLenient;
1247 }
1248 
1250 
1251 #endif /* #if !UCONFIG_NO_FORMATTING */
1252 
1253 #endif // _NUMFMT
1254 //eof
Base class for all formats.
Definition: format.h:96
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1210
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1204
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:218
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:1135
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:188
C++ API: Unicode String.
Round towards positive infinity.
Definition: numfmt.h:186
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:1238
#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:137
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1198
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1160
Round towards negative infinity.
Definition: numfmt.h:187
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:1133
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Round away from zero.
Definition: numfmt.h:189
#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: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:396
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:185
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:286
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:223
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1244
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: Compatibility APIs for number formatting.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188