ICU 60.2 60.2

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

- File List
- Globals

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 * 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
174 class U_I18N_API NumberFormat : public Format {
175 public:
185 enum ERoundingMode {
186 kRoundCeiling,
187 kRoundFloor,
188 kRoundDown,
189 kRoundUp,
190 kRoundHalfEven,
192 kRoundHalfDown,
194 kRoundHalfUp,
200 kRoundUnnecessary
201 };
202
218 enum EAlignmentFields {
220 kIntegerField = UNUM_INTEGER_FIELD,
222 kFractionField = UNUM_FRACTION_FIELD,
224 kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
226 kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
228 kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
230 kExponentField = UNUM_EXPONENT_FIELD,
232 kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
234 kCurrencyField = UNUM_CURRENCY_FIELD,
236 kPercentField = UNUM_PERCENT_FIELD,
238 kPermillField = UNUM_PERMILL_FIELD,
240 kSignField = UNUM_SIGN_FIELD,
241
247 INTEGER_FIELD = UNUM_INTEGER_FIELD,
249 FRACTION_FIELD = UNUM_FRACTION_FIELD
250 };
251
256 virtual ~NumberFormat();
257
264 virtual UBool operator==(const Format& other) const;
265
266
267 using Format::format;
268
284 virtual UnicodeString& format(const Formattable& obj,
285 UnicodeString& appendTo,
286 FieldPosition& pos,
287 UErrorCode& status) const;
288
305 virtual UnicodeString& format(const Formattable& obj,
306 UnicodeString& appendTo,
307 FieldPositionIterator* posIter,
308 UErrorCode& status) const;
309
338 virtual void parseObject(const UnicodeString& source,
339 Formattable& result,
340 ParsePosition& parse_pos) const;
341
352 UnicodeString& format( double number,
353 UnicodeString& appendTo) const;
354
365 UnicodeString& format( int32_t number,
366 UnicodeString& appendTo) const;
367
378 UnicodeString& format( int64_t number,
379 UnicodeString& appendTo) const;
380
393 virtual UnicodeString& format(double number,
394 UnicodeString& appendTo,
395 FieldPosition& pos) const = 0;
410 virtual UnicodeString& format(double number,
411 UnicodeString& appendTo,
412 FieldPosition& pos,
413 UErrorCode &status) const;
428 virtual UnicodeString& format(double number,
429 UnicodeString& appendTo,
430 FieldPositionIterator* posIter,
431 UErrorCode& status) const;
444 virtual UnicodeString& format(int32_t number,
445 UnicodeString& appendTo,
446 FieldPosition& pos) const = 0;
447
461 virtual UnicodeString& format(int32_t number,
462 UnicodeString& appendTo,
463 FieldPosition& pos,
464 UErrorCode &status) const;
465
480 virtual UnicodeString& format(int32_t number,
481 UnicodeString& appendTo,
482 FieldPositionIterator* posIter,
483 UErrorCode& status) const;
497 virtual UnicodeString& format(int64_t number,
498 UnicodeString& appendTo,
499 FieldPosition& pos) const;
500
515 virtual UnicodeString& format(int64_t number,
516 UnicodeString& appendTo,
517 FieldPosition& pos,
518 UErrorCode& status) const;
533 virtual UnicodeString& format(int64_t number,
534 UnicodeString& appendTo,
535 FieldPositionIterator* posIter,
536 UErrorCode& status) const;
537
554 virtual UnicodeString& format(StringPiece number,
555 UnicodeString& appendTo,
556 FieldPositionIterator* posIter,
557 UErrorCode& status) const;
558 public:
576 virtual UnicodeString& format(const DigitList &number,
577 UnicodeString& appendTo,
578 FieldPositionIterator* posIter,
579 UErrorCode& status) const;
580
598 virtual UnicodeString& format(const DigitList &number,
599 UnicodeString& appendTo,
600 FieldPosition& pos,
601 UErrorCode& status) const;
602
603 public:
604
624 virtual void parse(const UnicodeString& text,
625 Formattable& result,
626 ParsePosition& parsePosition) const = 0;
627
641 virtual void parse(const UnicodeString& text,
642 Formattable& result,
643 UErrorCode& status) const;
644
664 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
665 ParsePosition& pos) const;
666
678 UBool isParseIntegerOnly(void) const;
679
687 virtual void setParseIntegerOnly(UBool value);
688
696 virtual void setLenient(UBool enable);
697
706 virtual UBool isLenient(void) const;
707
713 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
714
721 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
722 UErrorCode&);
723
732 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
733 UNumberFormatStyle style,
734 UErrorCode& errorCode);
735
736 #ifndef U_HIDE_INTERNAL_API
737
743 static NumberFormat* internalCreateInstance(
744 const Locale& desiredLocale,
745 UNumberFormatStyle style,
746 UErrorCode& errorCode);
747
755 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
756 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
757
758 #endif /* U_HIDE_INTERNAL_API */
759
764 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
765
771 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
772 UErrorCode&);
773
778 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
779
785 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
786 UErrorCode&);
787
792 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
793
799 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
800 UErrorCode&);
801
807 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
808
809 #if !UCONFIG_NO_SERVICE
810
820 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
821
834 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
835
842 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
843 #endif /* UCONFIG_NO_SERVICE */
844
854 UBool isGroupingUsed(void) const;
855
862 virtual void setGroupingUsed(UBool newValue);
863
872 int32_t getMaximumIntegerDigits(void) const;
873
886 virtual void setMaximumIntegerDigits(int32_t newValue);
887
896 int32_t getMinimumIntegerDigits(void) const;
897
908 virtual void setMinimumIntegerDigits(int32_t newValue);
909
918 int32_t getMaximumFractionDigits(void) const;
919
930 virtual void setMaximumFractionDigits(int32_t newValue);
931
940 int32_t getMinimumFractionDigits(void) const;
941
952 virtual void setMinimumFractionDigits(int32_t newValue);
953
966 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
967
975 const char16_t* getCurrency() const;
976
986 virtual void setContext(UDisplayContext value, UErrorCode& status);
987
998 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
999
1006 virtual ERoundingMode getRoundingMode(void) const;
1007
1013 virtual void setRoundingMode(ERoundingMode roundingMode);
1014
1015 public:
1016
1025 static UClassID U_EXPORT2 getStaticClassID(void);
1026
1038 virtual UClassID getDynamicClassID(void) const = 0;
1039
1040 protected:
1041
1046 NumberFormat();
1047
1052 NumberFormat(const NumberFormat&);
1053
1058 NumberFormat& operator=(const NumberFormat&);
1059
1068 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1069
1070 #ifndef U_HIDE_INTERNAL_API
1071
1077 static NumberFormat* makeInstance(const Locale& desiredLocale,
1078 UNumberFormatStyle style,
1079 UBool mustBeDecimalFormat,
1080 UErrorCode& errorCode);
1081 #endif /* U_HIDE_INTERNAL_API */
1082
1083 private:
1084
1085 static UBool isStyleSupported(UNumberFormatStyle style);
1086
1094 static NumberFormat* makeInstance(const Locale& desiredLocale,
1095 UNumberFormatStyle style,
1096 UErrorCode& errorCode);
1097
1098 UBool fGroupingUsed;
1099 int32_t fMaxIntegerDigits;
1100 int32_t fMinIntegerDigits;
1101 int32_t fMaxFractionDigits;
1102 int32_t fMinFractionDigits;
1103
1104 protected:
1106 static const int32_t gDefaultMaxIntegerDigits;
1108 static const int32_t gDefaultMinIntegerDigits;
1109
1110 private:
1111 UBool fParseIntegerOnly;
1112 UBool fLenient; // TRUE => lenient parse is enabled
1113
1114 // ISO currency code
1115 char16_t fCurrency[4];
1116
1117 UDisplayContext fCapitalizationContext;
1118
1119 friend class ICUNumberFormatFactory; // access to makeInstance
1120 friend class ICUNumberFormatService;
1121 friend class ::NumberFormatTest; // access to isStyleSupported()
1122 };
1123
1124 #if !UCONFIG_NO_SERVICE
1125
1133 class U_I18N_API NumberFormatFactory : public UObject {
1134 public:
1135
1140 virtual ~NumberFormatFactory();
1141
1148 virtual UBool visible(void) const = 0;
1149
1155 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1156
1164 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1165 };
1166
1171 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1172 protected:
1177 const UBool _visible;
1178
1183 UnicodeString _id;
1184
1185 public:
1189 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1190
1194 virtual ~SimpleNumberFormatFactory();
1195
1199 virtual UBool visible(void) const;
1200
1204 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1205 };
1206 #endif /* #if !UCONFIG_NO_SERVICE */
1207
1208 // -------------------------------------
1209
1210 inline UBool
1211 NumberFormat::isParseIntegerOnly() const
1212 {
1213 return fParseIntegerOnly;
1214 }
1215
1216 inline UBool
1217 NumberFormat::isLenient() const
1218 {
1219 return fLenient;
1220 }
1221
1222 U_NAMESPACE_END
1223
1224 #endif /* #if !UCONFIG_NO_FORMATTING */
1225
1226 #endif // _NUMFMT
1227 //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:1183
UNUM_PERMILL_FIELD
Definition: unum.h:371
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1177
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:218
icu::NumberFormat::gDefaultMinIntegerDigits
static const int32_t gDefaultMinIntegerDigits
Definition: numfmt.h:1108
icu::NumberFormat
Abstract base class for all number formats.
Definition: numfmt.h:174
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:64
icu::NumberFormat::kRoundDown
Round towards zero.
Definition: numfmt.h:188
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_FIELD
Definition: unum.h:363
UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:359
UNUM_INTEGER_FIELD
Definition: unum.h:353
icu::NumberFormat::kRoundCeiling
Round towards positive infinity.
Definition: numfmt.h:186
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:1217
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:357
UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:361
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:1211
icu::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1171
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:142
icu::NumberFormatFactory
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1133
icu::NumberFormat::kRoundFloor
Round towards negative infinity.
Definition: numfmt.h:187
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:62
icu::NumberFormat::gDefaultMaxIntegerDigits
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1106
UNUM_FRACTION_FIELD
Definition: unum.h:355
UNUM_CURRENCY_FIELD
Definition: unum.h:367
icu::NumberFormat::kRoundUp
Round away from zero.
Definition: numfmt.h:189
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:240
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:373
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:369
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::NumberFormat::ERoundingMode
ERoundingMode
Rounding mode.
Definition: numfmt.h:185
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:286
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:365
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:236
unum.h
C API: NumberFormat.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188