ICU 63.1 63.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
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
246 INTEGER_FIELD = UNUM_INTEGER_FIELD,
248 FRACTION_FIELD = UNUM_FRACTION_FIELD
249 };
250
255 virtual ~NumberFormat();
256
263 virtual UBool operator==(const Format& other) const;
264
265
266 using Format::format;
267
283 virtual UnicodeString& format(const Formattable& obj,
284 UnicodeString& appendTo,
285 FieldPosition& pos,
286 UErrorCode& status) const;
287
304 virtual UnicodeString& format(const Formattable& obj,
305 UnicodeString& appendTo,
306 FieldPositionIterator* posIter,
307 UErrorCode& status) const;
308
337 virtual void parseObject(const UnicodeString& source,
338 Formattable& result,
339 ParsePosition& parse_pos) const;
340
351 UnicodeString& format( double number,
352 UnicodeString& appendTo) const;
353
364 UnicodeString& format( int32_t number,
365 UnicodeString& appendTo) const;
366
377 UnicodeString& format( int64_t number,
378 UnicodeString& appendTo) const;
379
392 virtual UnicodeString& format(double number,
393 UnicodeString& appendTo,
394 FieldPosition& pos) const = 0;
409 virtual UnicodeString& format(double number,
410 UnicodeString& appendTo,
411 FieldPosition& pos,
412 UErrorCode &status) const;
427 virtual UnicodeString& format(double number,
428 UnicodeString& appendTo,
429 FieldPositionIterator* posIter,
430 UErrorCode& status) const;
443 virtual UnicodeString& format(int32_t number,
444 UnicodeString& appendTo,
445 FieldPosition& pos) const = 0;
446
460 virtual UnicodeString& format(int32_t number,
461 UnicodeString& appendTo,
462 FieldPosition& pos,
463 UErrorCode &status) const;
464
479 virtual UnicodeString& format(int32_t number,
480 UnicodeString& appendTo,
481 FieldPositionIterator* posIter,
482 UErrorCode& status) const;
496 virtual UnicodeString& format(int64_t number,
497 UnicodeString& appendTo,
498 FieldPosition& pos) const;
499
514 virtual UnicodeString& format(int64_t number,
515 UnicodeString& appendTo,
516 FieldPosition& pos,
517 UErrorCode& status) const;
532 virtual UnicodeString& format(int64_t number,
533 UnicodeString& appendTo,
534 FieldPositionIterator* posIter,
535 UErrorCode& status) const;
536
553 virtual UnicodeString& format(StringPiece number,
554 UnicodeString& appendTo,
555 FieldPositionIterator* posIter,
556 UErrorCode& status) const;
557
558 // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
559
577 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
578 UnicodeString& appendTo,
579 FieldPositionIterator* posIter,
580 UErrorCode& status) const;
581
599 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
600 UnicodeString& appendTo,
601 FieldPosition& pos,
602 UErrorCode& status) const;
603
623 virtual void parse(const UnicodeString& text,
624 Formattable& result,
625 ParsePosition& parsePosition) const = 0;
626
640 virtual void parse(const UnicodeString& text,
641 Formattable& result,
642 UErrorCode& status) const;
643
663 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
664 ParsePosition& pos) const;
665
677 UBool isParseIntegerOnly(void) const;
678
686 virtual void setParseIntegerOnly(UBool value);
687
695 virtual void setLenient(UBool enable);
696
705 virtual UBool isLenient(void) const;
706
715 static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
716
726 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
727 UErrorCode&);
728
740 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
741 UNumberFormatStyle style,
742 UErrorCode& errorCode);
743
744 #ifndef U_HIDE_INTERNAL_API
745
751 static NumberFormat* internalCreateInstance(
752 const Locale& desiredLocale,
753 UNumberFormatStyle style,
754 UErrorCode& errorCode);
755
763 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
764 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
765
766 #endif /* U_HIDE_INTERNAL_API */
767
775 static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
776
785 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
786 UErrorCode&);
787
795 static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
796
805 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
806 UErrorCode&);
807
815 static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
816
825 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
826 UErrorCode&);
827
833 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
834
835 #if !UCONFIG_NO_SERVICE
836
846 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
847
860 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
861
868 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
869 #endif /* UCONFIG_NO_SERVICE */
870
880 UBool isGroupingUsed(void) const;
881
888 virtual void setGroupingUsed(UBool newValue);
889
898 int32_t getMaximumIntegerDigits(void) const;
899
912 virtual void setMaximumIntegerDigits(int32_t newValue);
913
922 int32_t getMinimumIntegerDigits(void) const;
923
934 virtual void setMinimumIntegerDigits(int32_t newValue);
935
944 int32_t getMaximumFractionDigits(void) const;
945
956 virtual void setMaximumFractionDigits(int32_t newValue);
957
966 int32_t getMinimumFractionDigits(void) const;
967
978 virtual void setMinimumFractionDigits(int32_t newValue);
979
992 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
993
1001 const char16_t* getCurrency() const;
1002
1012 virtual void setContext(UDisplayContext value, UErrorCode& status);
1013
1024 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
1025
1032 virtual ERoundingMode getRoundingMode(void) const;
1033
1039 virtual void setRoundingMode(ERoundingMode roundingMode);
1040
1041 public:
1042
1051 static UClassID U_EXPORT2 getStaticClassID(void);
1052
1064 virtual UClassID getDynamicClassID(void) const = 0;
1065
1066 protected:
1067
1072 NumberFormat();
1073
1078 NumberFormat(const NumberFormat&);
1079
1084 NumberFormat& operator=(const NumberFormat&);
1085
1094 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1095
1096 #ifndef U_HIDE_INTERNAL_API
1097
1103 static NumberFormat* makeInstance(const Locale& desiredLocale,
1104 UNumberFormatStyle style,
1105 UBool mustBeDecimalFormat,
1106 UErrorCode& errorCode);
1107 #endif /* U_HIDE_INTERNAL_API */
1108
1109 private:
1110
1111 static UBool isStyleSupported(UNumberFormatStyle style);
1112
1120 static NumberFormat* makeInstance(const Locale& desiredLocale,
1121 UNumberFormatStyle style,
1122 UErrorCode& errorCode);
1123
1124 UBool fGroupingUsed;
1125 int32_t fMaxIntegerDigits;
1126 int32_t fMinIntegerDigits;
1127 int32_t fMaxFractionDigits;
1128 int32_t fMinFractionDigits;
1129
1130 protected:
1132 static const int32_t gDefaultMaxIntegerDigits;
1134 static const int32_t gDefaultMinIntegerDigits;
1135
1136 private:
1137 UBool fParseIntegerOnly;
1138 UBool fLenient; // TRUE => lenient parse is enabled
1139
1140 // ISO currency code
1141 char16_t fCurrency[4];
1142
1143 UDisplayContext fCapitalizationContext;
1144
1145 friend class ICUNumberFormatFactory; // access to makeInstance
1146 friend class ICUNumberFormatService;
1147 friend class ::NumberFormatTest; // access to isStyleSupported()
1148 };
1149
1150 #if !UCONFIG_NO_SERVICE
1151
1159 class U_I18N_API NumberFormatFactory : public UObject {
1160 public:
1161
1166 virtual ~NumberFormatFactory();
1167
1174 virtual UBool visible(void) const = 0;
1175
1181 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1182
1190 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1191 };
1192
1197 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
1198 protected:
1203 const UBool _visible;
1204
1209 UnicodeString _id;
1210
1211 public:
1215 SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
1216
1220 virtual ~SimpleNumberFormatFactory();
1221
1225 virtual UBool visible(void) const;
1226
1230 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
1231 };
1232 #endif /* #if !UCONFIG_NO_SERVICE */
1233
1234 // -------------------------------------
1235
1236 inline UBool
1237 NumberFormat::isParseIntegerOnly() const
1238 {
1239 return fParseIntegerOnly;
1240 }
1241
1242 inline UBool
1243 NumberFormat::isLenient() const
1244 {
1245 return fLenient;
1246 }
1247
1248 U_NAMESPACE_END
1249
1250 #endif /* #if !UCONFIG_NO_FORMATTING */
1251
1252 #endif // _NUMFMT
1253 //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:1209
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:1203
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:1134
icu::NumberFormat
Definition: numfmt.h:173
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&#39;s RTTI.
Definition: uobject.h:90
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)
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:1197
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:1159
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:1132
UNUM_FRACTION_FIELD
Definition: unum.h:359
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: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:220
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