ICU 56.1: numfmt.h Source File

ICU 56.1 56.1

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

- File List
- Globals

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"
40 #include "unicode/udisplaycontext.h"
41
42 class NumberFormatTest;
43
44 U_NAMESPACE_BEGIN
45
46 class SharedNumberFormat;
47
48 #if !UCONFIG_NO_SERVICE
49 class NumberFormatFactory;
50 class StringEnumeration;
51 #endif
52
172 class U_I18N_API NumberFormat : public Format {
173 public:
189 enum EAlignmentFields {
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
634 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
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
686 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
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
718 static NumberFormat* internalCreateInstance(
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
739 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
740
746 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
747 UErrorCode&);
748
753 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
754
760 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
761 UErrorCode&);
762
767 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
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
973 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
974
975 public:
976
985 static UClassID U_EXPORT2 getStaticClassID(void);
986
998 virtual UClassID getDynamicClassID(void) const = 0;
999
1000 protected:
1001
1006 NumberFormat();
1007
1012 NumberFormat(const NumberFormat&);
1013
1018 NumberFormat& operator=(const NumberFormat&);
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
1091 class U_I18N_API NumberFormatFactory : public UObject {
1092 public:
1093
1098 virtual ~NumberFormatFactory();
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
1129 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1130 protected:
1135 const UBool _visible;
1136
1141 UnicodeString _id;
1142
1143 public:
1147 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1148
1152 virtual ~SimpleNumberFormatFactory();
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
1180 U_NAMESPACE_END
1181
1182 #endif /* #if !UCONFIG_NO_FORMATTING */
1183
1184 #endif // _NUMFMT
1185 //eof
icu::Format
Base class for all formats.
Definition: format.h:94
icu::SimpleNumberFormatFactory::_id
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1141
UNUM_PERMILL_FIELD
Definition: unum.h:357
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1135
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:189
icu::NumberFormat
Abstract base class for all number formats.
Definition: numfmt.h:172
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:56
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_FIELD
Definition: unum.h:349
UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:345
UNUM_INTEGER_FIELD
Definition: unum.h:339
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
icu::StringEnumeration
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:55
udisplaycontext.h
C API: Display context types (enum values)
format.h
C++ API: Base class for all formats.
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
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:343
UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:347
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::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1129
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:139
icu::NumberFormatFactory
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1091
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:53
UNUM_FRACTION_FIELD
Definition: unum.h:341
UNUM_CURRENCY_FIELD
Definition: unum.h:353
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:238
URegistryKey
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration...
Definition: umisc.h:55
UNUM_SIGN_FIELD
Definition: unum.h:359
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
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:25
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
UNUM_PERCENT_FIELD
Definition: unum.h:355
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
locid.h
C++ API: Locale ID 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:293
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
icu::NumberFormatFactory::getSupportedIDs
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
icu::NumberFormatFactory::visible
virtual UBool visible(void) const =0
Return true if this factory will be visible.
UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:351
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:221
icu::CurrencyAmount
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:35
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
unum.h
C API: NumberFormat.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185