ICU 70.1  70.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 * Deprecated 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 
29 #if U_SHOW_CPLUSPLUS_API
30 
36 #if !UCONFIG_NO_FORMATTING
37 
38 #include "unicode/unistr.h"
39 #include "unicode/format.h"
40 #include "unicode/unum.h" // UNumberFormatStyle
41 #include "unicode/locid.h"
42 #include "unicode/stringpiece.h"
43 #include "unicode/curramt.h"
45 
46 class NumberFormatTest;
47 
48 U_NAMESPACE_BEGIN
49 
50 class SharedNumberFormat;
51 
52 #if !UCONFIG_NO_SERVICE
53 class NumberFormatFactory;
54 class StringEnumeration;
55 #endif
56 
176 public:
191  kRoundHalfEven,
193  kRoundHalfDown,
195  kRoundHalfUp,
201  kRoundUnnecessary
202  };
203 
221  kIntegerField = UNUM_INTEGER_FIELD,
223  kFractionField = UNUM_FRACTION_FIELD,
225  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
227  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
229  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
231  kExponentField = UNUM_EXPONENT_FIELD,
233  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
235  kCurrencyField = UNUM_CURRENCY_FIELD,
237  kPercentField = UNUM_PERCENT_FIELD,
239  kPermillField = UNUM_PERMILL_FIELD,
241  kSignField = UNUM_SIGN_FIELD,
243  kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
245  kCompactField = UNUM_COMPACT_FIELD,
246 
252  INTEGER_FIELD = UNUM_INTEGER_FIELD,
254  FRACTION_FIELD = UNUM_FRACTION_FIELD
255  };
256 
261  virtual ~NumberFormat();
262 
269  virtual NumberFormat* clone() const override = 0;
270 
277  virtual bool operator==(const Format& other) const override;
278 
279 
280  using Format::format;
281 
297  virtual UnicodeString& format(const Formattable& obj,
298  UnicodeString& appendTo,
299  FieldPosition& pos,
300  UErrorCode& status) const override;
301 
318  virtual UnicodeString& format(const Formattable& obj,
319  UnicodeString& appendTo,
320  FieldPositionIterator* posIter,
321  UErrorCode& status) const override;
322 
351  virtual void parseObject(const UnicodeString& source,
352  Formattable& result,
353  ParsePosition& parse_pos) const override;
354 
365  UnicodeString& format( double number,
366  UnicodeString& appendTo) const;
367 
378  UnicodeString& format( int32_t number,
379  UnicodeString& appendTo) const;
380 
391  UnicodeString& format( int64_t number,
392  UnicodeString& appendTo) const;
393 
406  virtual UnicodeString& format(double number,
407  UnicodeString& appendTo,
408  FieldPosition& pos) const = 0;
423  virtual UnicodeString& format(double number,
424  UnicodeString& appendTo,
425  FieldPosition& pos,
426  UErrorCode &status) const;
441  virtual UnicodeString& format(double number,
442  UnicodeString& appendTo,
443  FieldPositionIterator* posIter,
444  UErrorCode& status) const;
457  virtual UnicodeString& format(int32_t number,
458  UnicodeString& appendTo,
459  FieldPosition& pos) const = 0;
460 
474  virtual UnicodeString& format(int32_t number,
475  UnicodeString& appendTo,
476  FieldPosition& pos,
477  UErrorCode &status) const;
478 
493  virtual UnicodeString& format(int32_t number,
494  UnicodeString& appendTo,
495  FieldPositionIterator* posIter,
496  UErrorCode& status) const;
510  virtual UnicodeString& format(int64_t number,
511  UnicodeString& appendTo,
512  FieldPosition& pos) const;
513 
528  virtual UnicodeString& format(int64_t number,
529  UnicodeString& appendTo,
530  FieldPosition& pos,
531  UErrorCode& status) const;
546  virtual UnicodeString& format(int64_t number,
547  UnicodeString& appendTo,
548  FieldPositionIterator* posIter,
549  UErrorCode& status) const;
550 
568  UnicodeString& appendTo,
569  FieldPositionIterator* posIter,
570  UErrorCode& status) const;
571 
572 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
573 
591  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
592  UnicodeString& appendTo,
593  FieldPositionIterator* posIter,
594  UErrorCode& status) const;
595 
613  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
614  UnicodeString& appendTo,
615  FieldPosition& pos,
616  UErrorCode& status) const;
617 
637  virtual void parse(const UnicodeString& text,
638  Formattable& result,
639  ParsePosition& parsePosition) const = 0;
640 
656  virtual void parse(const UnicodeString& text,
657  Formattable& result,
658  UErrorCode& status) const;
659 
680  ParsePosition& pos) const;
681 
693  UBool isParseIntegerOnly(void) const;
694 
702  virtual void setParseIntegerOnly(UBool value);
703 
711  virtual void setLenient(UBool enable);
712 
721  virtual UBool isLenient(void) const;
722 
732 
742  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
743  UErrorCode&);
744 
756  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
757  UNumberFormatStyle style,
758  UErrorCode& errorCode);
759 
760 #ifndef U_HIDE_INTERNAL_API
761 
768  const Locale& desiredLocale,
769  UNumberFormatStyle style,
770  UErrorCode& errorCode);
771 
779  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
780  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
781 
782 #endif /* U_HIDE_INTERNAL_API */
783 
792 
801  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
802  UErrorCode&);
803 
812 
821  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
822  UErrorCode&);
823 
832 
841  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
842  UErrorCode&);
843 
849  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
850 
851 #if !UCONFIG_NO_SERVICE
862  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
863 
876  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
877 
884  static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
885 #endif /* UCONFIG_NO_SERVICE */
886 
896  UBool isGroupingUsed(void) const;
897 
904  virtual void setGroupingUsed(UBool newValue);
905 
914  int32_t getMaximumIntegerDigits(void) const;
915 
928  virtual void setMaximumIntegerDigits(int32_t newValue);
929 
938  int32_t getMinimumIntegerDigits(void) const;
939 
950  virtual void setMinimumIntegerDigits(int32_t newValue);
951 
960  int32_t getMaximumFractionDigits(void) const;
961 
972  virtual void setMaximumFractionDigits(int32_t newValue);
973 
982  int32_t getMinimumFractionDigits(void) const;
983 
994  virtual void setMinimumFractionDigits(int32_t newValue);
995 
1008  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
1009 
1017  const char16_t* getCurrency() const;
1018 
1028  virtual void setContext(UDisplayContext value, UErrorCode& status);
1029 
1041 
1048  virtual ERoundingMode getRoundingMode(void) const;
1049 
1055  virtual void setRoundingMode(ERoundingMode roundingMode);
1056 
1057 public:
1058 
1067  static UClassID U_EXPORT2 getStaticClassID(void);
1068 
1080  virtual UClassID getDynamicClassID(void) const override = 0;
1081 
1082 protected:
1083 
1089 
1095 
1101 
1110  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1111 
1112 #ifndef U_HIDE_INTERNAL_API
1119  static NumberFormat* makeInstance(const Locale& desiredLocale,
1120  UNumberFormatStyle style,
1121  UBool mustBeDecimalFormat,
1122  UErrorCode& errorCode);
1123 #endif /* U_HIDE_INTERNAL_API */
1124 
1125 private:
1126 
1127  static UBool isStyleSupported(UNumberFormatStyle style);
1128 
1136  static NumberFormat* makeInstance(const Locale& desiredLocale,
1137  UNumberFormatStyle style,
1138  UErrorCode& errorCode);
1139 
1140  UBool fGroupingUsed;
1141  int32_t fMaxIntegerDigits;
1142  int32_t fMinIntegerDigits;
1143  int32_t fMaxFractionDigits;
1144  int32_t fMinFractionDigits;
1145 
1146  protected:
1148  static const int32_t gDefaultMaxIntegerDigits;
1150  static const int32_t gDefaultMinIntegerDigits;
1151 
1152  private:
1153  UBool fParseIntegerOnly;
1154  UBool fLenient; // true => lenient parse is enabled
1155 
1156  // ISO currency code
1157  char16_t fCurrency[4];
1158 
1159  UDisplayContext fCapitalizationContext;
1160 
1161  friend class ICUNumberFormatFactory; // access to makeInstance
1162  friend class ICUNumberFormatService;
1163  friend class ::NumberFormatTest; // access to isStyleSupported()
1164 };
1165 
1166 #if !UCONFIG_NO_SERVICE
1176 public:
1177 
1183 
1190  virtual UBool visible(void) const = 0;
1191 
1197  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1198 
1206  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1207 };
1208 
1214 protected:
1220 
1226 
1227 public:
1231  SimpleNumberFormatFactory(const Locale& locale, UBool visible = true);
1232 
1237 
1241  virtual UBool visible(void) const override;
1242 
1246  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const override;
1247 };
1248 #endif /* #if !UCONFIG_NO_SERVICE */
1249 
1250 // -------------------------------------
1251 
1252 inline UBool
1253 NumberFormat::isParseIntegerOnly() const
1254 {
1255  return fParseIntegerOnly;
1256 }
1257 
1258 inline UBool
1259 NumberFormat::isLenient() const
1260 {
1261  return fLenient;
1262 }
1263 
1264 U_NAMESPACE_END
1265 
1266 #endif /* #if !UCONFIG_NO_FORMATTING */
1267 
1268 #endif /* U_SHOW_CPLUSPLUS_API */
1269 
1270 #endif // _NUMFMT
1271 //eof
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:39
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1175
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
virtual NumberFormat * createFormat(const Locale &loc, UNumberFormatStyle formatType)=0
Return a number format of the appropriate type.
virtual ~NumberFormatFactory()
Destructor.
virtual UBool visible(void) const =0
Return true if this factory will be visible.
virtual void getEffectiveCurrency(char16_t *result, UErrorCode &ec) const
Returns the currency in effect for this formatter.
virtual void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
static NumberFormat * createInstance(UErrorCode &)
Create a default style NumberFormat for the current default locale.
virtual void setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an int64 number.
static const Locale * getAvailableLocales(int32_t &count)
Get the set of Locales for which NumberFormats are installed.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a long number.
virtual void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode &status) const
Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYP...
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
virtual NumberFormat * clone() const override=0
Clones this object polymorphically.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
Parse a string to produce an object.
virtual ERoundingMode getRoundingMode(void) const
Get the rounding mode.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format an object to produce a string.
static NumberFormat * internalCreateInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
ICU use only.
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1148
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
static NumberFormat * createScientificInstance(UErrorCode &)
Returns a scientific format for the current default locale.
UnicodeString & format(int32_t number, UnicodeString &appendTo) const
Format a long number.
static NumberFormat * createScientificInstance(const Locale &inLocale, UErrorCode &)
Returns a scientific format for the specified locale.
static StringEnumeration * getAvailableLocales(void)
Return a StringEnumeration over the locales available at the time of the call, including registered l...
static NumberFormat * makeInstance(const Locale &desiredLocale, UNumberFormatStyle style, UBool mustBeDecimalFormat, UErrorCode &errorCode)
Creates the specified number format style of the desired locale.
static NumberFormat * createInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
Create a specific style NumberFormat for the specified locale.
int32_t getMinimumFractionDigits(void) const
Returns the minimum number of digits allowed in the fraction portion of a number.
virtual ~NumberFormat()
Destructor.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos) const =0
Format a long number.
UnicodeString & format(int64_t number, UnicodeString &appendTo) const
Format an int64 number.
virtual void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
virtual CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const
Parses text from the given string as a currency amount.
NumberFormat()
Default constructor for subclass use only.
static const int32_t gDefaultMinIntegerDigits
Definition: numfmt.h:1150
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:219
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a double number.
virtual void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
static URegistryKey registerFactory(NumberFormatFactory *toAdopt, UErrorCode &status)
Register a new NumberFormatFactory.
virtual void setCurrency(const char16_t *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
const char16_t * getCurrency() const
Gets the currency used to display currency amounts.
virtual void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
virtual UClassID getDynamicClassID(void) const override=0
Returns a unique class ID POLYMORPHICALLY.
static const SharedNumberFormat * createSharedInstance(const Locale &inLocale, UNumberFormatStyle style, UErrorCode &status)
ICU use only.
ERoundingMode
Rounding mode.
Definition: numfmt.h:186
@ kRoundDown
Round towards zero.
Definition: numfmt.h:189
@ kRoundFloor
Round towards negative infinity.
Definition: numfmt.h:188
@ kRoundCeiling
Round towards positive infinity.
Definition: numfmt.h:187
@ kRoundUp
Round away from zero.
Definition: numfmt.h:190
int32_t getMaximumIntegerDigits(void) const
Returns the maximum number of digits allowed in the integer portion of a number.
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a decimal number.
virtual UnicodeString & format(StringPiece number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
static NumberFormat * createPercentInstance(const Locale &inLocale, UErrorCode &)
Returns a percentage format for the specified locale.
int32_t getMaximumFractionDigits(void) const
Returns the maximum number of digits allowed in the fraction portion of a number.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format an int32 number.
static NumberFormat * createCurrencyInstance(const Locale &inLocale, UErrorCode &)
Returns a currency format for the specified locale.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos) const =0
Format a double number.
static UBool unregister(URegistryKey key, UErrorCode &status)
Unregister a previously-registered NumberFormatFactory using the key returned from the register call.
UnicodeString & format(double number, UnicodeString &appendTo) const
Format a double number.
static NumberFormat * createPercentInstance(UErrorCode &)
Returns a percentage format for the current default locale.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a double number.
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos) const
Format an int64 number.
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
UBool isGroupingUsed(void) const
Returns true if grouping is used in this format.
static NumberFormat * createCurrencyInstance(UErrorCode &)
Returns a currency format for the current default locale.
NumberFormat(const NumberFormat &)
Copy constructor.
virtual void parse(const UnicodeString &text, Formattable &result, UErrorCode &status) const
Parse a string as a numeric value, and return a Formattable numeric object.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format an int64 number.
static UClassID getStaticClassID(void)
Return the class ID for this class.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format an object to produce a string.
int32_t getMinimumIntegerDigits(void) const
Returns the minimum number of digits allowed in the integer portion of a number.
static NumberFormat * createInstance(const Locale &inLocale, UErrorCode &)
Create a default style NumberFormat for the specified locale.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1213
SimpleNumberFormatFactory(const Locale &locale, UBool visible=true)
virtual UBool visible(void) const override
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const override
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1225
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1219
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: Currency Amount Object.
C++ API: Base class for all formats.
C++ API: Locale ID object.
C++ API: StringPiece: Read-only byte string wrapper class.
C API: Display context types (enum values)
UDisplayContextType
Display context types, for getting values of a particular setting.
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration.
Definition: umisc.h:57
C++ API: Unicode String.
C API: Compatibility APIs for number formatting.
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:149
@ UNUM_MEASURE_UNIT_FIELD
Definition: unum.h:401
@ UNUM_COMPACT_FIELD
Definition: unum.h:403
@ UNUM_INTEGER_FIELD
Definition: unum.h:379
@ UNUM_PERCENT_FIELD
Definition: unum.h:395
@ UNUM_PERMILL_FIELD
Definition: unum.h:397
@ UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:385
@ UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:391
@ UNUM_DECIMAL_SEPARATOR_FIELD
Definition: unum.h:383
@ UNUM_SIGN_FIELD
Definition: unum.h:399
@ UNUM_CURRENCY_FIELD
Definition: unum.h:393
@ UNUM_FRACTION_FIELD
Definition: unum.h:381
@ UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:387
@ UNUM_EXPONENT_FIELD
Definition: unum.h:389
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301