ICU 58.2 58.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

numfmt.h
Go to the documentation of this file.
1 // Copyright (C) 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"
42 #include "unicode/udisplaycontext.h"
43
44 class NumberFormatTest;
45
46 U_NAMESPACE_BEGIN
47
48 class SharedNumberFormat;
49
50 #if !UCONFIG_NO_SERVICE
51 class NumberFormatFactory;
52 class StringEnumeration;
53 #endif
54
169 class U_I18N_API NumberFormat : public Format {
170 public:
186 enum EAlignmentFields {
188 kIntegerField = UNUM_INTEGER_FIELD,
190 kFractionField = UNUM_FRACTION_FIELD,
192 kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
194 kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
196 kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
198 kExponentField = UNUM_EXPONENT_FIELD,
200 kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
202 kCurrencyField = UNUM_CURRENCY_FIELD,
204 kPercentField = UNUM_PERCENT_FIELD,
206 kPermillField = UNUM_PERMILL_FIELD,
208 kSignField = UNUM_SIGN_FIELD,
209
215 INTEGER_FIELD = UNUM_INTEGER_FIELD,
217 FRACTION_FIELD = UNUM_FRACTION_FIELD
218 };
219
224 virtual ~NumberFormat();
225
232 virtual UBool operator==(const Format& other) const;
233
234
235 using Format::format;
236
252 virtual UnicodeString& format(const Formattable& obj,
253 UnicodeString& appendTo,
254 FieldPosition& pos,
255 UErrorCode& status) const;
256
273 virtual UnicodeString& format(const Formattable& obj,
274 UnicodeString& appendTo,
275 FieldPositionIterator* posIter,
276 UErrorCode& status) const;
277
306 virtual void parseObject(const UnicodeString& source,
307 Formattable& result,
308 ParsePosition& parse_pos) const;
309
320 UnicodeString& format( double number,
321 UnicodeString& appendTo) const;
322
333 UnicodeString& format( int32_t number,
334 UnicodeString& appendTo) const;
335
346 UnicodeString& format( int64_t number,
347 UnicodeString& appendTo) const;
348
361 virtual UnicodeString& format(double number,
362 UnicodeString& appendTo,
363 FieldPosition& pos) const = 0;
378 virtual UnicodeString& format(double number,
379 UnicodeString& appendTo,
380 FieldPosition& pos,
381 UErrorCode &status) const;
396 virtual UnicodeString& format(double number,
397 UnicodeString& appendTo,
398 FieldPositionIterator* posIter,
399 UErrorCode& status) const;
412 virtual UnicodeString& format(int32_t number,
413 UnicodeString& appendTo,
414 FieldPosition& pos) const = 0;
415
429 virtual UnicodeString& format(int32_t number,
430 UnicodeString& appendTo,
431 FieldPosition& pos,
432 UErrorCode &status) const;
433
448 virtual UnicodeString& format(int32_t number,
449 UnicodeString& appendTo,
450 FieldPositionIterator* posIter,
451 UErrorCode& status) const;
465 virtual UnicodeString& format(int64_t number,
466 UnicodeString& appendTo,
467 FieldPosition& pos) const;
468
483 virtual UnicodeString& format(int64_t number,
484 UnicodeString& appendTo,
485 FieldPosition& pos,
486 UErrorCode& status) const;
501 virtual UnicodeString& format(int64_t number,
502 UnicodeString& appendTo,
503 FieldPositionIterator* posIter,
504 UErrorCode& status) const;
505
522 virtual UnicodeString& format(StringPiece number,
523 UnicodeString& appendTo,
524 FieldPositionIterator* posIter,
525 UErrorCode& status) const;
526 public:
544 virtual UnicodeString& format(const DigitList &number,
545 UnicodeString& appendTo,
546 FieldPositionIterator* posIter,
547 UErrorCode& status) const;
548
566 virtual UnicodeString& format(const DigitList &number,
567 UnicodeString& appendTo,
568 FieldPosition& pos,
569 UErrorCode& status) const;
570
571 public:
572
592 virtual void parse(const UnicodeString& text,
593 Formattable& result,
594 ParsePosition& parsePosition) const = 0;
595
609 virtual void parse(const UnicodeString& text,
610 Formattable& result,
611 UErrorCode& status) const;
612
632 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
633 ParsePosition& pos) const;
634
646 UBool isParseIntegerOnly(void) const;
647
655 virtual void setParseIntegerOnly(UBool value);
656
664 virtual void setLenient(UBool enable);
665
674 virtual UBool isLenient(void) const;
675
681 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
682
689 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
690 UErrorCode&);
691
700 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
701 UNumberFormatStyle style,
702 UErrorCode& errorCode);
703
704 #ifndef U_HIDE_INTERNAL_API
705
711 static NumberFormat* internalCreateInstance(
712 const Locale& desiredLocale,
713 UNumberFormatStyle style,
714 UErrorCode& errorCode);
715
723 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
724 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
725
726 #endif /* U_HIDE_INTERNAL_API */
727
732 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
733
739 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
740 UErrorCode&);
741
746 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
747
753 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
754 UErrorCode&);
755
760 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
761
767 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
768 UErrorCode&);
769
775 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
776
777 #if !UCONFIG_NO_SERVICE
778
788 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
789
802 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
803
810 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
811 #endif /* UCONFIG_NO_SERVICE */
812
822 UBool isGroupingUsed(void) const;
823
830 virtual void setGroupingUsed(UBool newValue);
831
840 int32_t getMaximumIntegerDigits(void) const;
841
854 virtual void setMaximumIntegerDigits(int32_t newValue);
855
864 int32_t getMinimumIntegerDigits(void) const;
865
876 virtual void setMinimumIntegerDigits(int32_t newValue);
877
886 int32_t getMaximumFractionDigits(void) const;
887
898 virtual void setMaximumFractionDigits(int32_t newValue);
899
908 int32_t getMinimumFractionDigits(void) const;
909
920 virtual void setMinimumFractionDigits(int32_t newValue);
921
934 virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
935
943 const UChar* getCurrency() const;
944
954 virtual void setContext(UDisplayContext value, UErrorCode& status);
955
966 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
967
968 public:
969
978 static UClassID U_EXPORT2 getStaticClassID(void);
979
991 virtual UClassID getDynamicClassID(void) const = 0;
992
993 protected:
994
999 NumberFormat();
1000
1005 NumberFormat(const NumberFormat&);
1006
1011 NumberFormat& operator=(const NumberFormat&);
1012
1021 virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
1022
1023 #ifndef U_HIDE_INTERNAL_API
1024
1030 static NumberFormat* makeInstance(const Locale& desiredLocale,
1031 UNumberFormatStyle style,
1032 UBool mustBeDecimalFormat,
1033 UErrorCode& errorCode);
1034 #endif /* U_HIDE_INTERNAL_API */
1035
1036 private:
1037
1038 static UBool isStyleSupported(UNumberFormatStyle style);
1039
1047 static NumberFormat* makeInstance(const Locale& desiredLocale,
1048 UNumberFormatStyle style,
1049 UErrorCode& errorCode);
1050
1051 UBool fGroupingUsed;
1052 int32_t fMaxIntegerDigits;
1053 int32_t fMinIntegerDigits;
1054 int32_t fMaxFractionDigits;
1055 int32_t fMinFractionDigits;
1056
1057 protected:
1059 static const int32_t gDefaultMaxIntegerDigits;
1061 static const int32_t gDefaultMinIntegerDigits;
1062
1063 private:
1064 UBool fParseIntegerOnly;
1065 UBool fLenient; // TRUE => lenient parse is enabled
1066
1067 // ISO currency code
1068 UChar fCurrency[4];
1069
1070 UDisplayContext fCapitalizationContext;
1071
1072 friend class ICUNumberFormatFactory; // access to makeInstance
1073 friend class ICUNumberFormatService;
1074 friend class ::NumberFormatTest; // access to isStyleSupported()
1075 };
1076
1077 #if !UCONFIG_NO_SERVICE
1078
1086 class U_I18N_API NumberFormatFactory : public UObject {
1087 public:
1088
1093 virtual ~NumberFormatFactory();
1094
1101 virtual UBool visible(void) const = 0;
1102
1108 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1109
1117 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1118 };
1119
1124 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1125 protected:
1130 const UBool _visible;
1131
1136 UnicodeString _id;
1137
1138 public:
1142 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1143
1147 virtual ~SimpleNumberFormatFactory();
1148
1152 virtual UBool visible(void) const;
1153
1157 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1158 };
1159 #endif /* #if !UCONFIG_NO_SERVICE */
1160
1161 // -------------------------------------
1162
1163 inline UBool
1164 NumberFormat::isParseIntegerOnly() const
1165 {
1166 return fParseIntegerOnly;
1167 }
1168
1169 inline UBool
1170 NumberFormat::isLenient() const
1171 {
1172 return fLenient;
1173 }
1174
1175 U_NAMESPACE_END
1176
1177 #endif /* #if !UCONFIG_NO_FORMATTING */
1178
1179 #endif // _NUMFMT
1180 //eof
icu::Format
Base class for all formats.
Definition: format.h:96
icu::SimpleNumberFormatFactory::_id
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1136
UNUM_PERMILL_FIELD
Definition: unum.h:365
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1130
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:186
icu::NumberFormat::gDefaultMinIntegerDigits
static const int32_t gDefaultMinIntegerDigits
Definition: numfmt.h:1061
icu::NumberFormat
Abstract base class for all number formats.
Definition: numfmt.h:169
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
icu::NumberFormatFactory::getSupportedIDs
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:66
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_FIELD
Definition: unum.h:357
UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:353
UNUM_INTEGER_FIELD
Definition: unum.h:347
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:93
icu::StringEnumeration
Base class for &#39;pure&#39; C++ implementations of uenum api.
Definition: strenum.h:57
udisplaycontext.h
C API: Display context types (enum values)
icu::NumberFormat::isLenient
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1170
format.h
C++ API: Base class for all formats.
icu::NumberFormatFactory::visible
virtual UBool visible(void) const =0
Return true if this factory will be visible.
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:360
stringpiece.h
C++ API: StringPiece: Read-only byte string wrapper class.
curramt.h
C++ API: Currency Amount Object.
UNUM_DECIMAL_SEPARATOR_FIELD
Definition: unum.h:351
UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:355
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
icu::NumberFormat::isParseIntegerOnly
UBool isParseIntegerOnly(void) const
Return true if this format will parse numbers as integers only.
Definition: numfmt.h:1164
icu::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1124
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:141
icu::NumberFormatFactory
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1086
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
icu::NumberFormat::gDefaultMaxIntegerDigits
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1059
UNUM_FRACTION_FIELD
Definition: unum.h:349
UNUM_CURRENCY_FIELD
Definition: unum.h:361
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:263
URegistryKey
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration...
Definition: umisc.h:57
UNUM_SIGN_FIELD
Definition: unum.h:367
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:337
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:27
UNUM_PERCENT_FIELD
Definition: unum.h:363
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
locid.h
C++ API: Locale ID object.
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::Format::operator=
Format & operator=(const Format &)
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:68
UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:359
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::CurrencyAmount
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:37
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259
unum.h
C API: NumberFormat.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:187