ICU 55.1  55.1
numfmt.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2015, 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"
41 
42 class NumberFormatTest;
43 
45 
46 class SharedNumberFormat;
47 
48 #if !UCONFIG_NO_SERVICE
49 class NumberFormatFactory;
50 class StringEnumeration;
51 #endif
52 
173 public:
191  kIntegerField = UNUM_INTEGER_FIELD,
193  kFractionField = UNUM_FRACTION_FIELD,
195  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
197  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
199  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
201  kExponentField = UNUM_EXPONENT_FIELD,
203  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
205  kCurrencyField = UNUM_CURRENCY_FIELD,
207  kPercentField = UNUM_PERCENT_FIELD,
209  kPermillField = UNUM_PERMILL_FIELD,
211  kSignField = UNUM_SIGN_FIELD,
212 
218  INTEGER_FIELD = UNUM_INTEGER_FIELD,
220  FRACTION_FIELD = UNUM_FRACTION_FIELD
221  };
222 
227  virtual ~NumberFormat();
228 
235  virtual UBool operator==(const Format& other) const;
236 
237 
238  using Format::format;
239 
255  virtual UnicodeString& format(const Formattable& obj,
256  UnicodeString& appendTo,
257  FieldPosition& pos,
258  UErrorCode& status) const;
259 
276  virtual UnicodeString& format(const Formattable& obj,
277  UnicodeString& appendTo,
278  FieldPositionIterator* posIter,
279  UErrorCode& status) const;
280 
309  virtual void parseObject(const UnicodeString& source,
310  Formattable& result,
311  ParsePosition& parse_pos) const;
312 
323  UnicodeString& format( double number,
324  UnicodeString& appendTo) const;
325 
336  UnicodeString& format( int32_t number,
337  UnicodeString& appendTo) const;
338 
349  UnicodeString& format( int64_t number,
350  UnicodeString& appendTo) const;
351 
364  virtual UnicodeString& format(double number,
365  UnicodeString& appendTo,
366  FieldPosition& pos) const = 0;
381  virtual UnicodeString& format(double number,
382  UnicodeString& appendTo,
383  FieldPosition& pos,
384  UErrorCode &status) const;
399  virtual UnicodeString& format(double number,
400  UnicodeString& appendTo,
401  FieldPositionIterator* posIter,
402  UErrorCode& status) const;
415  virtual UnicodeString& format(int32_t number,
416  UnicodeString& appendTo,
417  FieldPosition& pos) const = 0;
418 
432  virtual UnicodeString& format(int32_t number,
433  UnicodeString& appendTo,
434  FieldPosition& pos,
435  UErrorCode &status) const;
436 
451  virtual UnicodeString& format(int32_t number,
452  UnicodeString& appendTo,
453  FieldPositionIterator* posIter,
454  UErrorCode& status) const;
468  virtual UnicodeString& format(int64_t number,
469  UnicodeString& appendTo,
470  FieldPosition& pos) const;
471 
485  virtual UnicodeString& format(int64_t number,
486  UnicodeString& appendTo,
487  FieldPosition& pos,
488  UErrorCode& status) const;
503  virtual UnicodeString& format(int64_t number,
504  UnicodeString& appendTo,
505  FieldPositionIterator* posIter,
506  UErrorCode& status) const;
507 
524  virtual UnicodeString& format(const StringPiece &number,
525  UnicodeString& appendTo,
526  FieldPositionIterator* posIter,
527  UErrorCode& status) const;
528 public:
546  virtual UnicodeString& format(const DigitList &number,
547  UnicodeString& appendTo,
548  FieldPositionIterator* posIter,
549  UErrorCode& status) const;
550 
568  virtual UnicodeString& format(const DigitList &number,
569  UnicodeString& appendTo,
570  FieldPosition& pos,
571  UErrorCode& status) const;
572 
573 public:
574 
594  virtual void parse(const UnicodeString& text,
595  Formattable& result,
596  ParsePosition& parsePosition) const = 0;
597 
611  virtual void parse(const UnicodeString& text,
612  Formattable& result,
613  UErrorCode& status) const;
614 
635  ParsePosition& pos) const;
636 
648  UBool isParseIntegerOnly(void) const;
649 
657  virtual void setParseIntegerOnly(UBool value);
658 
666  virtual void setLenient(UBool enable);
667 
676  virtual UBool isLenient(void) const;
677 
687 
696  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
697  UErrorCode&);
698 
707  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
708  UNumberFormatStyle style,
709  UErrorCode& errorCode);
710 
711 #ifndef U_HIDE_INTERNAL_API
712 
719  const Locale& desiredLocale,
720  UNumberFormatStyle style,
721  UErrorCode& errorCode);
722 
730  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
731  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
732 
733 #endif /* U_HIDE_INTERNAL_API */
734 
740 
746  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
747  UErrorCode&);
748 
754 
760  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
761  UErrorCode&);
762 
768 
774  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
775  UErrorCode&);
776 
782  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
783 
784 #if !UCONFIG_NO_SERVICE
785 
795  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
796 
809  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
810 
817  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
818 #endif /* UCONFIG_NO_SERVICE */
819 
829  UBool isGroupingUsed(void) const;
830 
837  virtual void setGroupingUsed(UBool newValue);
838 
847  int32_t getMaximumIntegerDigits(void) const;
848 
861  virtual void setMaximumIntegerDigits(int32_t newValue);
862 
871  int32_t getMinimumIntegerDigits(void) const;
872 
883  virtual void setMinimumIntegerDigits(int32_t newValue);
884 
893  int32_t getMaximumFractionDigits(void) const;
894 
905  virtual void setMaximumFractionDigits(int32_t newValue);
906 
915  int32_t getMinimumFractionDigits(void) const;
916 
927  virtual void setMinimumFractionDigits(int32_t newValue);
928 
941  virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
942 
950  const UChar* getCurrency() const;
951 
961  virtual void setContext(UDisplayContext value, UErrorCode& status);
962 
974 
975 public:
976 
985  static UClassID U_EXPORT2 getStaticClassID(void);
986 
998  virtual UClassID getDynamicClassID(void) const = 0;
999 
1000 protected:
1001 
1007 
1013 
1019 
1028  virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
1029 
1030 #ifndef U_HIDE_INTERNAL_API
1031 
1037  static NumberFormat* makeInstance(const Locale& desiredLocale,
1038  UNumberFormatStyle style,
1039  UBool mustBeDecimalFormat,
1040  UErrorCode& errorCode);
1041 #endif /* U_HIDE_INTERNAL_API */
1042 
1043 private:
1044 
1045  static UBool isStyleSupported(UNumberFormatStyle style);
1046 
1054  static NumberFormat* makeInstance(const Locale& desiredLocale,
1055  UNumberFormatStyle style,
1056  UErrorCode& errorCode);
1057 
1058  UBool fGroupingUsed;
1059  int32_t fMaxIntegerDigits;
1060  int32_t fMinIntegerDigits;
1061  int32_t fMaxFractionDigits;
1062  int32_t fMinFractionDigits;
1063 
1064  protected:
1065  static const int32_t gDefaultMaxIntegerDigits;
1066  static const int32_t gDefaultMinIntegerDigits;
1067 
1068  private:
1069  UBool fParseIntegerOnly;
1070  UBool fLenient; // TRUE => lenient parse is enabled
1071 
1072  // ISO currency code
1073  UChar fCurrency[4];
1074 
1075  UDisplayContext fCapitalizationContext;
1076 
1077  friend class ICUNumberFormatFactory; // access to makeInstance
1078  friend class ICUNumberFormatService;
1079  friend class ::NumberFormatTest; // access to isStyleSupported()
1080 };
1081 
1082 #if !UCONFIG_NO_SERVICE
1083 
1092 public:
1093 
1099 
1106  virtual UBool visible(void) const = 0;
1107 
1113  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1114 
1122  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1123 };
1124 
1130 protected:
1136 
1142 
1143 public:
1147  SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1148 
1153 
1157  virtual UBool visible(void) const;
1158 
1162  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1163 };
1164 #endif /* #if !UCONFIG_NO_SERVICE */
1165 
1166 // -------------------------------------
1167 
1168 inline UBool
1169 NumberFormat::isParseIntegerOnly() const
1170 {
1171  return fParseIntegerOnly;
1172 }
1173 
1174 inline UBool
1175 NumberFormat::isLenient() const
1176 {
1177  return fLenient;
1178 }
1179 
1181 
1182 #endif /* #if !UCONFIG_NO_FORMATTING */
1183 
1184 #endif // _NUMFMT
1185 //eof
icu::NumberFormat::getMinimumIntegerDigits
int32_t getMinimumIntegerDigits(void) const
Returns the minimum number of digits allowed in the integer portion of a number.
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
icu::SimpleNumberFormatFactory::_id
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1141
icu::SimpleNumberFormatFactory::getSupportedIDs
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const
icu::NumberFormat
Abstract base class for all number formats.
Definition: numfmt.h:172
UNUM_INTEGER_FIELD
@ UNUM_INTEGER_FIELD
Definition: unum.h:313
icu::SimpleNumberFormatFactory::visible
virtual UBool visible(void) const
icu::NumberFormat::isGroupingUsed
UBool isGroupingUsed(void) const
Returns true if grouping is used in this format.
icu::NumberFormat::createCurrencyInstance
static NumberFormat * createCurrencyInstance(const Locale &inLocale, UErrorCode &)
Returns a currency format for the specified locale.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:358
icu::NumberFormat::parseCurrency
virtual CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const
Parses text from the given string as a currency amount.
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
UNUM_EXPONENT_SIGN_FIELD
@ UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:321
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:53
format.h
C++ API: Base class for all formats.
icu::NumberFormat::setMaximumIntegerDigits
virtual void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
icu::NumberFormat::getMaximumIntegerDigits
int32_t getMaximumIntegerDigits(void) const
Returns the maximum number of digits allowed in the integer portion of a number.
icu::NumberFormat::NumberFormat
NumberFormat(const NumberFormat &)
Copy constructor.
icu::NumberFormat::setMaximumFractionDigits
virtual void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::NumberFormat::operator=
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
UNUM_DECIMAL_SEPARATOR_FIELD
@ UNUM_DECIMAL_SEPARATOR_FIELD
Definition: unum.h:317
icu::NumberFormat::NumberFormat
NumberFormat()
Default constructor for subclass use only.
icu::NumberFormat::setParseIntegerOnly
virtual void setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
icu::NumberFormat::createCurrencyInstance
static NumberFormat * createCurrencyInstance(UErrorCode &)
Returns a currency format for the current default locale.
icu::NumberFormatFactory::createFormat
virtual NumberFormat * createFormat(const Locale &loc, UNumberFormatStyle formatType)=0
Return a number format of the appropriate type.
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:25
UNUM_FRACTION_FIELD
@ UNUM_FRACTION_FIELD
Definition: unum.h:315
UNUM_CURRENCY_FIELD
@ UNUM_CURRENCY_FIELD
Definition: unum.h:327
icu::NumberFormat::getContext
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode &status) const
Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYP...
UNUM_PERMILL_FIELD
@ UNUM_PERMILL_FIELD
Definition: unum.h:331
icu::Format
Base class for all formats.
Definition: format.h:94
icu::NumberFormat::internalCreateInstance
static NumberFormat * internalCreateInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
ICU use only.
stringpiece.h
C++ API: StringPiece: Read-only byte string wrapper class.
unum.h
C API: NumberFormat.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:246
UNUM_EXPONENT_FIELD
@ UNUM_EXPONENT_FIELD
Definition: unum.h:323
URegistryKey
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration.
Definition: umisc.h:55
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:221
icu::NumberFormatFactory::getSupportedIDs
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
icu::NumberFormat::setContext
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:476
icu::StringEnumeration
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:55
icu::NumberFormat::createInstance
static NumberFormat * createInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
Creates the specified decimal format style of the desired locale.
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:238
icu::NumberFormat::getMinimumFractionDigits
int32_t getMinimumFractionDigits(void) const
Returns the minimum number of digits allowed in the fraction portion of a number.
icu::NumberFormat::~NumberFormat
virtual ~NumberFormat()
Destructor.
icu::NumberFormat::registerFactory
static URegistryKey registerFactory(NumberFormatFactory *toAdopt, UErrorCode &status)
Register a new NumberFormatFactory.
icu::NumberFormat::operator==
virtual UBool operator==(const Format &other) const
Return true if the given Format objects are semantically equal.
icu::NumberFormatFactory::visible
virtual UBool visible(void) const =0
Return true if this factory will be visible.
icu::NumberFormat::getAvailableLocales
static const Locale * getAvailableLocales(int32_t &count)
Get the set of Locales for which NumberFormats are installed.
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:189
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:139
icu::NumberFormat::createInstance
static NumberFormat * createInstance(const Locale &inLocale, UErrorCode &)
Returns the default number format for the specified locale.
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1135
icu::NumberFormat::getEffectiveCurrency
virtual void getEffectiveCurrency(UChar *result, UErrorCode &ec) const
Returns the currency in effect for this formatter.
icu::NumberFormat::getStaticClassID
static UClassID getStaticClassID(void)
Return the class ID for this class.
icu::NumberFormat::setMinimumFractionDigits
virtual void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
UNUM_GROUPING_SEPARATOR_FIELD
@ UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:325
icu::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1129
curramt.h
C++ API: Currency Amount Object.
icu::NumberFormat::getAvailableLocales
static StringEnumeration * getAvailableLocales(void)
Return a StringEnumeration over the locales available at the time of the call, including registered l...
icu::NumberFormat::getCurrency
const UChar * getCurrency() const
Gets the currency used to display currency amounts.
icu::NumberFormat::createPercentInstance
static NumberFormat * createPercentInstance(UErrorCode &)
Returns a percentage format for the current default locale.
icu::NumberFormatFactory::~NumberFormatFactory
virtual ~NumberFormatFactory()
Destructor.
icu::NumberFormat::getMaximumFractionDigits
int32_t getMaximumFractionDigits(void) const
Returns the maximum number of digits allowed in the fraction portion of a number.
icu::NumberFormat::unregister
static UBool unregister(URegistryKey key, UErrorCode &status)
Unregister a previously-registered NumberFormatFactory using the key returned from the register call.
icu::NumberFormat::createInstance
static NumberFormat * createInstance(UErrorCode &)
Returns the default number format for the current default locale.
icu::NumberFormat::createScientificInstance
static NumberFormat * createScientificInstance(UErrorCode &)
Returns a scientific format for the current default locale.
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:59
icu::NumberFormat::getDynamicClassID
virtual UClassID getDynamicClassID(void) const =0
Returns a unique class ID POLYMORPHICALLY.
icu::NumberFormat::setCurrency
virtual void setCurrency(const UChar *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
icu::NumberFormat::createSharedInstance
static const SharedNumberFormat * createSharedInstance(const Locale &inLocale, UNumberFormatStyle style, UErrorCode &status)
ICU use only.
icu::NumberFormat::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
Parse a string to produce an object.
icu::NumberFormat::setLenient
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
icu::NumberFormat::setGroupingUsed
virtual void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
UNUM_SIGN_FIELD
@ UNUM_SIGN_FIELD
Definition: unum.h:333
UChar
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:312
udisplaycontext.h
C API: Display context types (enum values)
locid.h
C++ API: Locale ID object.
icu::SimpleNumberFormatFactory::~SimpleNumberFormatFactory
virtual ~SimpleNumberFormatFactory()
UNUM_PERCENT_FIELD
@ UNUM_PERCENT_FIELD
Definition: unum.h:329
icu::CurrencyAmount
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:35
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
icu::NumberFormat::setMinimumIntegerDigits
virtual void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
icu::NumberFormat::createScientificInstance
static NumberFormat * createScientificInstance(const Locale &inLocale, UErrorCode &)
Returns a scientific format for the specified locale.
icu::NumberFormatFactory
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1091
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185
icu::SimpleNumberFormatFactory::SimpleNumberFormatFactory
SimpleNumberFormatFactory(const Locale &locale, UBool visible=TRUE)
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_SYMBOL_FIELD
@ UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:319
icu::NumberFormat::createPercentInstance
static NumberFormat * createPercentInstance(const Locale &inLocale, UErrorCode &)
Returns a percentage format for the specified locale.
icu::NumberFormat::makeInstance
static NumberFormat * makeInstance(const Locale &desiredLocale, UNumberFormatStyle style, UBool mustBeDecimalFormat, UErrorCode &errorCode)
Creates the specified number format style of the desired locale.