ICU 76.1  76.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,
217  };
218 
236  kIntegerField = UNUM_INTEGER_FIELD,
238  kFractionField = UNUM_FRACTION_FIELD,
240  kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
242  kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
244  kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
246  kExponentField = UNUM_EXPONENT_FIELD,
248  kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
250  kCurrencyField = UNUM_CURRENCY_FIELD,
252  kPercentField = UNUM_PERCENT_FIELD,
254  kPermillField = UNUM_PERMILL_FIELD,
256  kSignField = UNUM_SIGN_FIELD,
258  kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
260  kCompactField = UNUM_COMPACT_FIELD,
261 
267  INTEGER_FIELD = UNUM_INTEGER_FIELD,
269  FRACTION_FIELD = UNUM_FRACTION_FIELD
270  };
271 
276  virtual ~NumberFormat();
277 
284  virtual NumberFormat* clone() const override = 0;
285 
292  virtual bool operator==(const Format& other) const override;
293 
294 
295  using Format::format;
296 
312  virtual UnicodeString& format(const Formattable& obj,
313  UnicodeString& appendTo,
314  FieldPosition& pos,
315  UErrorCode& status) const override;
316 
333  virtual UnicodeString& format(const Formattable& obj,
334  UnicodeString& appendTo,
335  FieldPositionIterator* posIter,
336  UErrorCode& status) const override;
337 
366  virtual void parseObject(const UnicodeString& source,
367  Formattable& result,
368  ParsePosition& parse_pos) const override;
369 
380  UnicodeString& format( double number,
381  UnicodeString& appendTo) const;
382 
393  UnicodeString& format( int32_t number,
394  UnicodeString& appendTo) const;
395 
406  UnicodeString& format( int64_t number,
407  UnicodeString& appendTo) const;
408 
421  virtual UnicodeString& format(double number,
422  UnicodeString& appendTo,
423  FieldPosition& pos) const = 0;
438  virtual UnicodeString& format(double number,
439  UnicodeString& appendTo,
440  FieldPosition& pos,
441  UErrorCode &status) const;
456  virtual UnicodeString& format(double number,
457  UnicodeString& appendTo,
458  FieldPositionIterator* posIter,
459  UErrorCode& status) const;
472  virtual UnicodeString& format(int32_t number,
473  UnicodeString& appendTo,
474  FieldPosition& pos) const = 0;
475 
489  virtual UnicodeString& format(int32_t number,
490  UnicodeString& appendTo,
491  FieldPosition& pos,
492  UErrorCode &status) const;
493 
508  virtual UnicodeString& format(int32_t number,
509  UnicodeString& appendTo,
510  FieldPositionIterator* posIter,
511  UErrorCode& status) const;
525  virtual UnicodeString& format(int64_t number,
526  UnicodeString& appendTo,
527  FieldPosition& pos) const;
528 
543  virtual UnicodeString& format(int64_t number,
544  UnicodeString& appendTo,
545  FieldPosition& pos,
546  UErrorCode& status) const;
561  virtual UnicodeString& format(int64_t number,
562  UnicodeString& appendTo,
563  FieldPositionIterator* posIter,
564  UErrorCode& status) const;
565 
582  virtual UnicodeString& format(StringPiece number,
583  UnicodeString& appendTo,
584  FieldPositionIterator* posIter,
585  UErrorCode& status) const;
586 
587 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
588 
606  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
607  UnicodeString& appendTo,
608  FieldPositionIterator* posIter,
609  UErrorCode& status) const;
610 
628  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
629  UnicodeString& appendTo,
630  FieldPosition& pos,
631  UErrorCode& status) const;
632 
652  virtual void parse(const UnicodeString& text,
653  Formattable& result,
654  ParsePosition& parsePosition) const = 0;
655 
671  virtual void parse(const UnicodeString& text,
672  Formattable& result,
673  UErrorCode& status) const;
674 
694  virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
695  ParsePosition& pos) const;
696 
708  UBool isParseIntegerOnly() const;
709 
717  virtual void setParseIntegerOnly(UBool value);
718 
726  virtual void setLenient(UBool enable);
727 
736  virtual UBool isLenient() const;
737 
746  static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
747 
757  static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
758  UErrorCode&);
759 
771  static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
772  UNumberFormatStyle style,
773  UErrorCode& errorCode);
774 
775 #ifndef U_HIDE_INTERNAL_API
776 
782  static NumberFormat* internalCreateInstance(
783  const Locale& desiredLocale,
784  UNumberFormatStyle style,
785  UErrorCode& errorCode);
786 
794  static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
795  const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
796 
797 #endif /* U_HIDE_INTERNAL_API */
798 
806  static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
807 
816  static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
817  UErrorCode&);
818 
826  static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
827 
836  static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
837  UErrorCode&);
838 
846  static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
847 
856  static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
857  UErrorCode&);
858 
864  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
865 
866 #if !UCONFIG_NO_SERVICE
867 
877  static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
878 
891  static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
892 
899  static StringEnumeration* U_EXPORT2 getAvailableLocales();
900 #endif /* UCONFIG_NO_SERVICE */
901 
911  UBool isGroupingUsed() const;
912 
919  virtual void setGroupingUsed(UBool newValue);
920 
929  int32_t getMaximumIntegerDigits() const;
930 
943  virtual void setMaximumIntegerDigits(int32_t newValue);
944 
953  int32_t getMinimumIntegerDigits() const;
954 
965  virtual void setMinimumIntegerDigits(int32_t newValue);
966 
975  int32_t getMaximumFractionDigits() const;
976 
987  virtual void setMaximumFractionDigits(int32_t newValue);
988 
997  int32_t getMinimumFractionDigits() const;
998 
1009  virtual void setMinimumFractionDigits(int32_t newValue);
1010 
1023  virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
1024 
1032  const char16_t* getCurrency() const;
1033 
1043  virtual void setContext(UDisplayContext value, UErrorCode& status);
1044 
1055  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1056 
1063  virtual ERoundingMode getRoundingMode() const;
1064 
1070  virtual void setRoundingMode(ERoundingMode roundingMode);
1071 
1072 public:
1073 
1082  static UClassID U_EXPORT2 getStaticClassID();
1083 
1095  virtual UClassID getDynamicClassID() const override = 0;
1096 
1097 protected:
1098 
1103  NumberFormat();
1104 
1109  NumberFormat(const NumberFormat&);
1110 
1116 
1125  virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1126 
1127 #ifndef U_HIDE_INTERNAL_API
1128 
1134  static NumberFormat* makeInstance(const Locale& desiredLocale,
1135  UNumberFormatStyle style,
1136  UBool mustBeDecimalFormat,
1137  UErrorCode& errorCode);
1138 #endif /* U_HIDE_INTERNAL_API */
1139 
1140 private:
1141 
1142  static UBool isStyleSupported(UNumberFormatStyle style);
1143 
1151  static NumberFormat* makeInstance(const Locale& desiredLocale,
1152  UNumberFormatStyle style,
1153  UErrorCode& errorCode);
1154 
1155  UBool fGroupingUsed;
1156  int32_t fMaxIntegerDigits;
1157  int32_t fMinIntegerDigits;
1158  int32_t fMaxFractionDigits;
1159  int32_t fMinFractionDigits;
1160 
1161  protected:
1163  static const int32_t gDefaultMaxIntegerDigits;
1165  static const int32_t gDefaultMinIntegerDigits;
1166 
1167  private:
1168  UBool fParseIntegerOnly;
1169  UBool fLenient; // true => lenient parse is enabled
1170 
1171  // ISO currency code
1172  char16_t fCurrency[4];
1173 
1174  UDisplayContext fCapitalizationContext;
1175 
1176  friend class ICUNumberFormatFactory; // access to makeInstance
1177  friend class ICUNumberFormatService;
1178  friend class ::NumberFormatTest; // access to isStyleSupported()
1179 };
1180 
1181 #if !UCONFIG_NO_SERVICE
1182 
1191 public:
1192 
1197  virtual ~NumberFormatFactory();
1198 
1205  virtual UBool visible() const = 0;
1206 
1212  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1213 
1221  virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1222 };
1223 
1229 protected:
1235 
1241 
1242 public:
1246  SimpleNumberFormatFactory(const Locale& locale, UBool visible = true);
1247 
1251  virtual ~SimpleNumberFormatFactory();
1252 
1256  virtual UBool visible() const override;
1257 
1261  virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const override;
1262 };
1263 #endif /* #if !UCONFIG_NO_SERVICE */
1264 
1265 // -------------------------------------
1266 
1267 inline UBool
1268 NumberFormat::isParseIntegerOnly() const
1269 {
1270  return fParseIntegerOnly;
1271 }
1272 
1273 inline UBool
1274 NumberFormat::isLenient() const
1275 {
1276  return fLenient;
1277 }
1278 
1279 U_NAMESPACE_END
1280 
1281 #endif /* #if !UCONFIG_NO_FORMATTING */
1282 
1283 #endif /* U_SHOW_CPLUSPLUS_API */
1284 
1285 #endif // _NUMFMT
1286 //eof
Base class for all formats.
Definition: format.h:98
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1240
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1234
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:234
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:1165
virtual bool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
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:189
C++ API: Unicode String.
Round towards positive infinity.
Definition: numfmt.h:187
Rounds ties toward the odd number.
Definition: numfmt.h:206
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Return U_FORMAT_INEXACT_ERROR if number does not format exactly.
Definition: numfmt.h:201
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
virtual Format * clone() const =0
Clone this object polymorphically.
C API: Display context types (enum values)
C++ API: Base class for all formats.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
C++ API: StringPiece: Read-only byte string wrapper class.
C++ API: Currency Amount Object.
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1228
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:150
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1190
Round towards negative infinity.
Definition: numfmt.h:188
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
virtual UBool visible() const =0
Return true if this factory will be visible.
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1163
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Round away from zero.
Definition: numfmt.h:190
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration...
Definition: umisc.h:57
UDisplayContextType
Display context types, for getting values of a particular setting.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
ERoundingMode
Rounding mode.
Definition: numfmt.h:186
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
C++ API: Locale ID object.
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:295
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:61
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:39
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
Rounds ties toward +∞.
Definition: numfmt.h:211
C API: Compatibility APIs for number formatting.
Rounds ties toward -∞.
Definition: numfmt.h:216
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195