ICU 64.2 64.2

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
173 class U_I18N_API NumberFormat : public Format {
174 public:
184 enum ERoundingMode {
185 kRoundCeiling,
186 kRoundFloor,
187 kRoundDown,
188 kRoundUp,
189 kRoundHalfEven,
191 kRoundHalfDown,
193 kRoundHalfUp,
199 kRoundUnnecessary
200 };
201
217 enum EAlignmentFields {
219 kIntegerField = UNUM_INTEGER_FIELD,
221 kFractionField = UNUM_FRACTION_FIELD,
223 kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
225 kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
227 kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
229 kExponentField = UNUM_EXPONENT_FIELD,
231 kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
233 kCurrencyField = UNUM_CURRENCY_FIELD,
235 kPercentField = UNUM_PERCENT_FIELD,
237 kPermillField = UNUM_PERMILL_FIELD,
239 kSignField = UNUM_SIGN_FIELD,
240 #ifndef U_HIDE_DRAFT_API
241
242 kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD,
244 kCompactField = UNUM_COMPACT_FIELD,
245 #endif // U_HIDE_DRAFT_API
246
252 INTEGER_FIELD = UNUM_INTEGER_FIELD,
254 FRACTION_FIELD = UNUM_FRACTION_FIELD
255 };
256
261 virtual ~NumberFormat();
262
269 virtual UBool operator==(const Format& other) const;
270
271
272 using Format::format;
273
289 virtual UnicodeString& format(const Formattable& obj,
290 UnicodeString& appendTo,
291 FieldPosition& pos,
292 UErrorCode& status) const;
293
310 virtual UnicodeString& format(const Formattable& obj,
311 UnicodeString& appendTo,
312 FieldPositionIterator* posIter,
313 UErrorCode& status) const;
314
343 virtual void parseObject(const UnicodeString& source,
344 Formattable& result,
345 ParsePosition& parse_pos) const;
346
357 UnicodeString& format( double number,
358 UnicodeString& appendTo) const;
359
370 UnicodeString& format( int32_t number,
371 UnicodeString& appendTo) const;
372
383 UnicodeString& format( int64_t number,
384 UnicodeString& appendTo) const;
385
398 virtual UnicodeString& format(double number,
399 UnicodeString& appendTo,
400 FieldPosition& pos) const = 0;
415 virtual UnicodeString& format(double number,
416 UnicodeString& appendTo,
417 FieldPosition& pos,
418 UErrorCode &status) const;
433 virtual UnicodeString& format(double number,
434 UnicodeString& appendTo,
435 FieldPositionIterator* posIter,
436 UErrorCode& status) const;
449 virtual UnicodeString& format(int32_t number,
450 UnicodeString& appendTo,
451 FieldPosition& pos) const = 0;
452
466 virtual UnicodeString& format(int32_t number,
467 UnicodeString& appendTo,
468 FieldPosition& pos,
469 UErrorCode &status) const;
470
485 virtual UnicodeString& format(int32_t number,
486 UnicodeString& appendTo,
487 FieldPositionIterator* posIter,
488 UErrorCode& status) const;
502 virtual UnicodeString& format(int64_t number,
503 UnicodeString& appendTo,
504 FieldPosition& pos) const;
505
520 virtual UnicodeString& format(int64_t number,
521 UnicodeString& appendTo,
522 FieldPosition& pos,
523 UErrorCode& status) const;
538 virtual UnicodeString& format(int64_t number,
539 UnicodeString& appendTo,
540 FieldPositionIterator* posIter,
541 UErrorCode& status) const;
542
559 virtual UnicodeString& format(StringPiece number,
560 UnicodeString& appendTo,
561 FieldPositionIterator* posIter,
562 UErrorCode& status) const;
563
564 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
565
583 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
584 UnicodeString& appendTo,
585 FieldPositionIterator* posIter,
586 UErrorCode& status) const;
587
605 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
606 UnicodeString& appendTo,
607 FieldPosition& pos,
608 UErrorCode& status) const;
609
629 virtual void parse(const UnicodeString& text,
630 Formattable& result,
631 ParsePosition& parsePosition) const = 0;
632
646 virtual void parse(const UnicodeString& text,
647 Formattable& result,
648 UErrorCode& status) const;
649
669 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
670 ParsePosition& pos) const;
671
683 UBool isParseIntegerOnly(void) const;
684
692 virtual void setParseIntegerOnly(UBool value);
693
701 virtual void setLenient(UBool enable);
702
711 virtual UBool isLenient(void) const;
712
721 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
722
732 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
733 UErrorCode&);
734
746 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
747 UNumberFormatStyle style,
748 UErrorCode& errorCode);
749
750 #ifndef U_HIDE_INTERNAL_API
751
757 static NumberFormat* internalCreateInstance(
758 const Locale& desiredLocale,
759 UNumberFormatStyle style,
760 UErrorCode& errorCode);
761
769 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
770 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
771
772 #endif /* U_HIDE_INTERNAL_API */
773
781 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
782
791 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
792 UErrorCode&);
793
801 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
802
811 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
812 UErrorCode&);
813
821 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
822
831 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
832 UErrorCode&);
833
839 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
840
841 #if !UCONFIG_NO_SERVICE
842
852 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
853
866 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
867
874 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
875 #endif /* UCONFIG_NO_SERVICE */
876
886 UBool isGroupingUsed(void) const;
887
894 virtual void setGroupingUsed(UBool newValue);
895
904 int32_t getMaximumIntegerDigits(void) const;
905
918 virtual void setMaximumIntegerDigits(int32_t newValue);
919
928 int32_t getMinimumIntegerDigits(void) const;
929
940 virtual void setMinimumIntegerDigits(int32_t newValue);
941
950 int32_t getMaximumFractionDigits(void) const;
951
962 virtual void setMaximumFractionDigits(int32_t newValue);
963
972 int32_t getMinimumFractionDigits(void) const;
973
984 virtual void setMinimumFractionDigits(int32_t newValue);
985
998 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
999
1007 const char16_t* getCurrency() const;
1008
1018 virtual void setContext(UDisplayContext value, UErrorCode& status);
1019
1030 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1031
1038 virtual ERoundingMode getRoundingMode(void) const;
1039
1045 virtual void setRoundingMode(ERoundingMode roundingMode);
1046
1047 public:
1048
1057 static UClassID U_EXPORT2 getStaticClassID(void);
1058
1070 virtual UClassID getDynamicClassID(void) const = 0;
1071
1072 protected:
1073
1078 NumberFormat();
1079
1084 NumberFormat(const NumberFormat&);
1085
1090 NumberFormat& operator=(const NumberFormat&);
1091
1100 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1101
1102 #ifndef U_HIDE_INTERNAL_API
1103
1109 static NumberFormat* makeInstance(const Locale& desiredLocale,
1110 UNumberFormatStyle style,
1111 UBool mustBeDecimalFormat,
1112 UErrorCode& errorCode);
1113 #endif /* U_HIDE_INTERNAL_API */
1114
1115 private:
1116
1117 static UBool isStyleSupported(UNumberFormatStyle style);
1118
1126 static NumberFormat* makeInstance(const Locale& desiredLocale,
1127 UNumberFormatStyle style,
1128 UErrorCode& errorCode);
1129
1130 UBool fGroupingUsed;
1131 int32_t fMaxIntegerDigits;
1132 int32_t fMinIntegerDigits;
1133 int32_t fMaxFractionDigits;
1134 int32_t fMinFractionDigits;
1135
1136 protected:
1138 static const int32_t gDefaultMaxIntegerDigits;
1140 static const int32_t gDefaultMinIntegerDigits;
1141
1142 private:
1143 UBool fParseIntegerOnly;
1144 UBool fLenient; // TRUE => lenient parse is enabled
1145
1146 // ISO currency code
1147 char16_t fCurrency[4];
1148
1149 UDisplayContext fCapitalizationContext;
1150
1151 friend class ICUNumberFormatFactory; // access to makeInstance
1152 friend class ICUNumberFormatService;
1153 friend class ::NumberFormatTest; // access to isStyleSupported()
1154 };
1155
1156 #if !UCONFIG_NO_SERVICE
1157
1165 class U_I18N_API NumberFormatFactory : public UObject {
1166 public:
1167
1172 virtual ~NumberFormatFactory();
1173
1180 virtual UBool visible(void) const = 0;
1181
1187 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1188
1196 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1197 };
1198
1203 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1204 protected:
1209 const UBool _visible;
1210
1215 UnicodeString _id;
1216
1217 public:
1221 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1222
1226 virtual ~SimpleNumberFormatFactory();
1227
1231 virtual UBool visible(void) const;
1232
1236 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1237 };
1238 #endif /* #if !UCONFIG_NO_SERVICE */
1239
1240 // -------------------------------------
1241
1242 inline UBool
1243 NumberFormat::isParseIntegerOnly() const
1244 {
1245 return fParseIntegerOnly;
1246 }
1247
1248 inline UBool
1249 NumberFormat::isLenient() const
1250 {
1251 return fLenient;
1252 }
1253
1254 U_NAMESPACE_END
1255
1256 #endif /* #if !UCONFIG_NO_FORMATTING */
1257
1258 #endif // _NUMFMT
1259 //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:1215
UNUM_PERMILL_FIELD
Definition: unum.h:375
icu::SimpleNumberFormatFactory::_visible
const UBool _visible
True if the locale supported by this factory is visible.
Definition: numfmt.h:1209
icu::NumberFormat::EAlignmentFields
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition: numfmt.h:217
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:1140
icu::NumberFormat
Definition: numfmt.h:173
UNUM_MEASURE_UNIT_FIELD
Definition: unum.h:380
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:187
unistr.h
C++ API: Unicode String.
UNUM_EXPONENT_FIELD
Definition: unum.h:367
UNUM_EXPONENT_SYMBOL_FIELD
Definition: unum.h:363
UNUM_INTEGER_FIELD
Definition: unum.h:357
icu::NumberFormat::kRoundCeiling
Round towards positive infinity.
Definition: numfmt.h:185
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
icu::StringEnumeration
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:57
UNUM_COMPACT_FIELD
Definition: unum.h:382
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.
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:361
UNUM_EXPONENT_SIGN_FIELD
Definition: unum.h:365
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale.
Definition: numfmt.h:1203
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:1165
icu::NumberFormat::kRoundFloor
Round towards negative infinity.
Definition: numfmt.h:186
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:1138
UNUM_FRACTION_FIELD
Definition: unum.h:359
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
UNUM_CURRENCY_FIELD
Definition: unum.h:371
icu::NumberFormat::kRoundUp
Round away from zero.
Definition: numfmt.h:188
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
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:377
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:27
UNUM_PERCENT_FIELD
Definition: unum.h:373
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:401
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:184
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:289
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
UNUM_GROUPING_SEPARATOR_FIELD
Definition: unum.h:369
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:225
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:192