ICU 65.1 65.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 * 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
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"
44 #include "unicode/udisplaycontext.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
175 class U_I18N_API NumberFormat : public Format {
176 public:
186 enum ERoundingMode {
187 kRoundCeiling,
188 kRoundFloor,
189 kRoundDown,
190 kRoundUp,
191 kRoundHalfEven,
193 kRoundHalfDown,
195 kRoundHalfUp,
201 kRoundUnnecessary
202 };
203
219 enum EAlignmentFields {
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,
242 #ifndef U_HIDE_DRAFT_API
243
244 kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
246 kCompactField = UNUM_COMPACT_FIELD,
247 #endif // U_HIDE_DRAFT_API
248
254 INTEGER_FIELD = UNUM_INTEGER_FIELD,
256 FRACTION_FIELD = UNUM_FRACTION_FIELD
257 };
258
263 virtual ~NumberFormat();
264
271 virtual NumberFormat* clone() const = 0;
272
279 virtual UBool operator==(const Format& other) const;
280
281
282 using Format::format;
283
299 virtual UnicodeString& format(const Formattable& obj,
300 UnicodeString& appendTo,
301 FieldPosition& pos,
302 UErrorCode& status) const;
303
320 virtual UnicodeString& format(const Formattable& obj,
321 UnicodeString& appendTo,
322 FieldPositionIterator* posIter,
323 UErrorCode& status) const;
324
353 virtual void parseObject(const UnicodeString& source,
354 Formattable& result,
355 ParsePosition& parse_pos) const;
356
367 UnicodeString& format( double number,
368 UnicodeString& appendTo) const;
369
380 UnicodeString& format( int32_t number,
381 UnicodeString& appendTo) const;
382
393 UnicodeString& format( int64_t number,
394 UnicodeString& appendTo) const;
395
408 virtual UnicodeString& format(double number,
409 UnicodeString& appendTo,
410 FieldPosition& pos) const = 0;
425 virtual UnicodeString& format(double number,
426 UnicodeString& appendTo,
427 FieldPosition& pos,
428 UErrorCode &status) const;
443 virtual UnicodeString& format(double number,
444 UnicodeString& appendTo,
445 FieldPositionIterator* posIter,
446 UErrorCode& status) const;
459 virtual UnicodeString& format(int32_t number,
460 UnicodeString& appendTo,
461 FieldPosition& pos) const = 0;
462
476 virtual UnicodeString& format(int32_t number,
477 UnicodeString& appendTo,
478 FieldPosition& pos,
479 UErrorCode &status) const;
480
495 virtual UnicodeString& format(int32_t number,
496 UnicodeString& appendTo,
497 FieldPositionIterator* posIter,
498 UErrorCode& status) const;
512 virtual UnicodeString& format(int64_t number,
513 UnicodeString& appendTo,
514 FieldPosition& pos) const;
515
530 virtual UnicodeString& format(int64_t number,
531 UnicodeString& appendTo,
532 FieldPosition& pos,
533 UErrorCode& status) const;
548 virtual UnicodeString& format(int64_t number,
549 UnicodeString& appendTo,
550 FieldPositionIterator* posIter,
551 UErrorCode& status) const;
552
569 virtual UnicodeString& format(StringPiece number,
570 UnicodeString& appendTo,
571 FieldPositionIterator* posIter,
572 UErrorCode& status) const;
573
574 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
575
593 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
594 UnicodeString& appendTo,
595 FieldPositionIterator* posIter,
596 UErrorCode& status) const;
597
615 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
616 UnicodeString& appendTo,
617 FieldPosition& pos,
618 UErrorCode& status) const;
619
639 virtual void parse(const UnicodeString& text,
640 Formattable& result,
641 ParsePosition& parsePosition) const = 0;
642
658 virtual void parse(const UnicodeString& text,
659 Formattable& result,
660 UErrorCode& status) const;
661
681 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
682 ParsePosition& pos) const;
683
695 UBool isParseIntegerOnly(void) const;
696
704 virtual void setParseIntegerOnly(UBool value);
705
713 virtual void setLenient(UBool enable);
714
723 virtual UBool isLenient(void) const;
724
733 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
734
744 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
745 UErrorCode&);
746
758 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
759 UNumberFormatStyle style,
760 UErrorCode& errorCode);
761
762 #ifndef U_HIDE_INTERNAL_API
763
769 static NumberFormat* internalCreateInstance(
770 const Locale& desiredLocale,
771 UNumberFormatStyle style,
772 UErrorCode& errorCode);
773
781 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
782 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
783
784 #endif /* U_HIDE_INTERNAL_API */
785
793 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
794
803 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
804 UErrorCode&);
805
813 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
814
823 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
824 UErrorCode&);
825
833 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
834
843 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
844 UErrorCode&);
845
851 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
852
853 #if !UCONFIG_NO_SERVICE
854
864 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
865
878 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
879
886 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
887 #endif /* UCONFIG_NO_SERVICE */
888
898 UBool isGroupingUsed(void) const;
899
906 virtual void setGroupingUsed(UBool newValue);
907
916 int32_t getMaximumIntegerDigits(void) const;
917
930 virtual void setMaximumIntegerDigits(int32_t newValue);
931
940 int32_t getMinimumIntegerDigits(void) const;
941
952 virtual void setMinimumIntegerDigits(int32_t newValue);
953
962 int32_t getMaximumFractionDigits(void) const;
963
974 virtual void setMaximumFractionDigits(int32_t newValue);
975
984 int32_t getMinimumFractionDigits(void) const;
985
996 virtual void setMinimumFractionDigits(int32_t newValue);
997
1010 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
1011
1019 const char16_t* getCurrency() const;
1020
1030 virtual void setContext(UDisplayContext value, UErrorCode& status);
1031
1042 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1043
1050 virtual ERoundingMode getRoundingMode(void) const;
1051
1057 virtual void setRoundingMode(ERoundingMode roundingMode);
1058
1059 public:
1060
1069 static UClassID U_EXPORT2 getStaticClassID(void);
1070
1082 virtual UClassID getDynamicClassID(void) const = 0;
1083
1084 protected:
1085
1090 NumberFormat();
1091
1096 NumberFormat(const NumberFormat&);
1097
1102 NumberFormat& operator=(const NumberFormat&);
1103
1112 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1113
1114 #ifndef U_HIDE_INTERNAL_API
1115
1121 static NumberFormat* makeInstance(const Locale& desiredLocale,
1122 UNumberFormatStyle style,
1123 UBool mustBeDecimalFormat,
1124 UErrorCode& errorCode);
1125 #endif /* U_HIDE_INTERNAL_API */
1126
1127 private:
1128
1129 static UBool isStyleSupported(UNumberFormatStyle style);
1130
1138 static NumberFormat* makeInstance(const Locale& desiredLocale,
1139 UNumberFormatStyle style,
1140 UErrorCode& errorCode);
1141
1142 UBool fGroupingUsed;
1143 int32_t fMaxIntegerDigits;
1144 int32_t fMinIntegerDigits;
1145 int32_t fMaxFractionDigits;
1146 int32_t fMinFractionDigits;
1147
1148 protected:
1150 static const int32_t gDefaultMaxIntegerDigits;
1152 static const int32_t gDefaultMinIntegerDigits;
1153
1154 private:
1155 UBool fParseIntegerOnly;
1156 UBool fLenient; // TRUE => lenient parse is enabled
1157
1158 // ISO currency code
1159 char16_t fCurrency[4];
1160
1161 UDisplayContext fCapitalizationContext;
1162
1163 friend class ICUNumberFormatFactory; // access to makeInstance
1164 friend class ICUNumberFormatService;
1165 friend class ::NumberFormatTest; // access to isStyleSupported()
1166 };
1167
1168 #if !UCONFIG_NO_SERVICE
1169
1177 class U_I18N_API NumberFormatFactory : public UObject {
1178 public:
1179
1184 virtual ~NumberFormatFactory();
1185
1192 virtual UBool visible(void) const = 0;
1193
1199 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1200
1208 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1209 };
1210
1215 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1216 protected:
1221 const UBool _visible;
1222
1227 UnicodeString _id;
1228
1229 public:
1233 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1234
1238 virtual ~SimpleNumberFormatFactory();
1239
1243 virtual UBool visible(void) const;
1244
1248 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1249 };
1250 #endif /* #if !UCONFIG_NO_SERVICE */
1251
1252 // -------------------------------------
1253
1254 inline UBool
1255 NumberFormat::isParseIntegerOnly() const
1256 {
1257 return fParseIntegerOnly;
1258 }
1259
1260 inline UBool
1261 NumberFormat::isLenient() const
1262 {
1263 return fLenient;
1264 }
1265
1266 U_NAMESPACE_END
1267
1268 #endif /* #if !UCONFIG_NO_FORMATTING */
1269
1270 #endif /* U_SHOW_CPLUSPLUS_API */
1271
1272 #endif // _NUMFMT
1273 //eof
icu::Format
Base class for all formats.
Definition: format.h:98
icu::SimpleNumberFormatFactory::_id
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition: numfmt.h:1227
UNUM_PERMILL_FIELD
Definition: unum.h:377
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1221
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:219
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::NumberFormat::gDefaultMinIntegerDigits
static const int32_t gDefaultMinIntegerDigits
Definition: numfmt.h:1152
icu::NumberFormat
Definition: numfmt.h:175
UNUM_MEASURE_UNIT_FIELD
Definition: unum.h:382
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:189
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_FIELD
Definition: unum.h:369
UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:365
UNUM_INTEGER_FIELD
Definition: unum.h:359
icu::NumberFormat::kRoundCeiling
Round towards positive infinity.
Definition: numfmt.h:187
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
icu::StringEnumeration
Base class for &#39;pure&#39; C++ implementations of uenum api.
Definition: strenum.h:61
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
UNUM_COMPACT_FIELD
Definition: unum.h:384
udisplaycontext.h
C API: Display context types (enum values)
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.
icu::NumberFormat::isParseIntegerOnly
UBool isParseIntegerOnly(void) const
Return true if this format will parse numbers as integers only.
Definition: numfmt.h:1255
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:301
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:363
UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:367
icu::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1215
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
icu::NumberFormatFactory
A NumberFormatFactory is used to register new number formats.
Definition: numfmt.h:1177
icu::NumberFormat::kRoundFloor
Round towards negative infinity.
Definition: numfmt.h:188
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
icu::NumberFormat::gDefaultMaxIntegerDigits
static const int32_t gDefaultMaxIntegerDigits
Definition: numfmt.h:1150
UNUM_FRACTION_FIELD
Definition: unum.h:361
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
UNUM_CURRENCY_FIELD
Definition: unum.h:373
icu::NumberFormat::kRoundUp
Round away from zero.
Definition: numfmt.h:190
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:265
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:379
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:27
UNUM_PERCENT_FIELD
Definition: unum.h:375
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
icu::NumberFormat::ERoundingMode
ERoundingMode
Rounding mode.
Definition: numfmt.h:186
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
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:294
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:371
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::NumberFormat::isLenient
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1261
icu::CurrencyAmount
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:39
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
unum.h
C API: Compatibility APIs for number formatting.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195