ICU 64.2  64.2
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 #ifndef U_HIDE_DRAFT_API
241 
242  kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
244  kCompactField = UNUM_COMPACT_FIELD,
245 #endif // U_HIDE_DRAFT_API
246 
252  INTEGER_FIELD = UNUM_INTEGER_FIELD,
254  FRACTION_FIELD = UNUM_FRACTION_FIELD
255  };
256 
261  virtual ~NumberFormat();
262 
269  virtual UBool operator==(const Format& other) const;
270 
271 
272  using Format::format;
273 
289  virtual UnicodeString& format(const Formattable& obj,
290  UnicodeString& appendTo,
291  FieldPosition& pos,
292  UErrorCode& status) const;
293 
310  virtual UnicodeString& format(const Formattable& obj,
311  UnicodeString& appendTo,
312  FieldPositionIterator* posIter,
313  UErrorCode& status) const;
314 
343  virtual void parseObject(const UnicodeString& source,
344  Formattable& result,
345  ParsePosition& parse_pos) const;
346 
357  UnicodeString& format( double number,
358  UnicodeString& appendTo) const;
359 
370  UnicodeString& format( int32_t number,
371  UnicodeString& appendTo) const;
372 
383  UnicodeString& format( int64_t number,
384  UnicodeString& appendTo) const;
385 
398  virtual UnicodeString& format(double number,
399  UnicodeString& appendTo,
400  FieldPosition& pos) const = 0;
415  virtual UnicodeString& format(double number,
416  UnicodeString& appendTo,
417  FieldPosition& pos,
418  UErrorCode &status) const;
433  virtual UnicodeString& format(double number,
434  UnicodeString& appendTo,
435  FieldPositionIterator* posIter,
436  UErrorCode& status) const;
449  virtual UnicodeString& format(int32_t number,
450  UnicodeString& appendTo,
451  FieldPosition& pos) const = 0;
452 
466  virtual UnicodeString& format(int32_t number,
467  UnicodeString& appendTo,
468  FieldPosition& pos,
469  UErrorCode &status) const;
470 
485  virtual UnicodeString& format(int32_t number,
486  UnicodeString& appendTo,
487  FieldPositionIterator* posIter,
488  UErrorCode& status) const;
502  virtual UnicodeString& format(int64_t number,
503  UnicodeString& appendTo,
504  FieldPosition& pos) const;
505 
520  virtual UnicodeString& format(int64_t number,
521  UnicodeString& appendTo,
522  FieldPosition& pos,
523  UErrorCode& status) const;
538  virtual UnicodeString& format(int64_t number,
539  UnicodeString& appendTo,
540  FieldPositionIterator* posIter,
541  UErrorCode& status) const;
542 
559  virtual UnicodeString& format(StringPiece number,
560  UnicodeString& appendTo,
561  FieldPositionIterator* posIter,
562  UErrorCode& status) const;
563 
564 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
565 
583  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
584  UnicodeString& appendTo,
585  FieldPositionIterator* posIter,
586  UErrorCode& status) const;
587 
605  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
606  UnicodeString& appendTo,
607  FieldPosition& pos,
608  UErrorCode& status) const;
609 
629  virtual void parse(const UnicodeString& text,
630  Formattable& result,
631  ParsePosition& parsePosition) const = 0;
632 
646  virtual void parse(const UnicodeString& text,
647  Formattable& result,
648  UErrorCode& status) const;
649 
669  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
670  ParsePosition& pos) const;
671 
683  UBool isParseIntegerOnly(void) const;
684 
692  virtual void setParseIntegerOnly(UBool value);
693 
701  virtual void setLenient(UBool enable);
702 
711  virtual UBool isLenient(void) const;
712 
721  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
722 
732  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
733  UErrorCode&);
734 
746  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
747  UNumberFormatStyle style,
748  UErrorCode& errorCode);
749 
750 #ifndef U_HIDE_INTERNAL_API
751 
757  static NumberFormat* internalCreateInstance(
758  const Locale& desiredLocale,
759  UNumberFormatStyle style,
760  UErrorCode& errorCode);
761 
769  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
770  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
771 
772 #endif /* U_HIDE_INTERNAL_API */
773 
781  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
782 
791  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
792  UErrorCode&);
793 
801  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
802 
811  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
812  UErrorCode&);
813 
821  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
822 
831  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
832  UErrorCode&);
833 
839  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
840 
841 #if !UCONFIG_NO_SERVICE
842 
852  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
853 
866  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
867 
874  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
875 #endif /* UCONFIG_NO_SERVICE */
876 
886  UBool isGroupingUsed(void) const;
887 
894  virtual void setGroupingUsed(UBool newValue);
895 
904  int32_t getMaximumIntegerDigits(void) const;
905 
918  virtual void setMaximumIntegerDigits(int32_t newValue);
919 
928  int32_t getMinimumIntegerDigits(void) const;
929 
940  virtual void setMinimumIntegerDigits(int32_t newValue);
941 
950  int32_t getMaximumFractionDigits(void) const;
951 
962  virtual void setMaximumFractionDigits(int32_t newValue);
963 
972  int32_t getMinimumFractionDigits(void) const;
973 
984  virtual void setMinimumFractionDigits(int32_t newValue);
985 
998  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
999 
1007  const char16_t* getCurrency() const;
1008 
1018  virtual void setContext(UDisplayContext value, UErrorCode& status);
1019 
1030  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1031 
1038  virtual ERoundingMode getRoundingMode(void) const;
1039 
1045  virtual void setRoundingMode(ERoundingMode roundingMode);
1046 
1047 public:
1048 
1057  static UClassID U_EXPORT2 getStaticClassID(void);
1058 
1070  virtual UClassID getDynamicClassID(void) const = 0;
1071 
1072 protected:
1073 
1078  NumberFormat();
1079 
1084  NumberFormat(const NumberFormat&);
1085 
1091 
1100  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1101 
1102 #ifndef U_HIDE_INTERNAL_API
1103 
1109  static NumberFormat* makeInstance(const Locale& desiredLocale,
1110  UNumberFormatStyle style,
1111  UBool mustBeDecimalFormat,
1112  UErrorCode& errorCode);
1113 #endif /* U_HIDE_INTERNAL_API */
1114 
1115 private:
1116 
1117  static UBool isStyleSupported(UNumberFormatStyle style);
1118 
1126  static NumberFormat* makeInstance(const Locale& desiredLocale,
1127  UNumberFormatStyle style,
1128  UErrorCode& errorCode);
1129 
1130  UBool fGroupingUsed;
1131  int32_t fMaxIntegerDigits;
1132  int32_t fMinIntegerDigits;
1133  int32_t fMaxFractionDigits;
1134  int32_t fMinFractionDigits;
1135 
1136  protected:
1138  static const int32_t gDefaultMaxIntegerDigits;
1140  static const int32_t gDefaultMinIntegerDigits;
1141 
1142  private:
1143  UBool fParseIntegerOnly;
1144  UBool fLenient; // TRUE => lenient parse is enabled
1145 
1146  // ISO currency code
1147  char16_t fCurrency[4];
1148 
1149  UDisplayContext fCapitalizationContext;
1150 
1151  friend class ICUNumberFormatFactory; // access to makeInstance
1152  friend class ICUNumberFormatService;
1153  friend class ::NumberFormatTest; // access to isStyleSupported()
1154 };
1155 
1156 #if !UCONFIG_NO_SERVICE
1157 
1166 public:
1167 
1172  virtual ~NumberFormatFactory();
1173 
1180  virtual UBool visible(void) const = 0;
1181 
1187  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1188 
1196  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1197 };
1198 
1204 protected:
1210 
1216 
1217 public:
1221  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1222 
1226  virtual ~SimpleNumberFormatFactory();
1227 
1231  virtual UBool visible(void) const;
1232 
1236  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1237 };
1238 #endif /* #if !UCONFIG_NO_SERVICE */
1239 
1240 // -------------------------------------
1241 
1242 inline UBool
1244 {
1245  return fParseIntegerOnly;
1246 }
1247 
1248 inline UBool
1250 {
1251  return fLenient;
1252 }
1253 
1255 
1256 #endif /* #if !UCONFIG_NO_FORMATTING */
1257 
1258 #endif // _NUMFMT
1259 //eof
Base class for all formats.
Definition: format.h:96
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1215
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1209
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:1140
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: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:1243
#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:1203
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1165
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:1138
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:223
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1249
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