ICU4C 79
Loading...
Searching...
No Matches
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* Deprecated 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
35
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"
45
46class NumberFormatTest;
47
48U_NAMESPACE_BEGIN
49
50class SharedNumberFormat;
51
52#if !UCONFIG_NO_SERVICE
53class NumberFormatFactory;
54class StringEnumeration;
55#endif
56
176public:
218
271
276 virtual ~NumberFormat();
277
284 virtual NumberFormat* clone() const override = 0;
285
292 virtual bool operator==(const Format& other) const override;
293
294
295 using Format::format;
296
312 virtual UnicodeString& format(const Formattable& obj,
313 UnicodeString& appendTo,
314 FieldPosition& pos,
315 UErrorCode& status) const override;
316
333 virtual UnicodeString& format(const Formattable& obj,
334 UnicodeString& appendTo,
335 FieldPositionIterator* posIter,
336 UErrorCode& status) const override;
337
363 virtual void parseObject(const UnicodeString& source,
364 Formattable& result,
365 ParsePosition& parse_pos) const override;
366
377 UnicodeString& format( double number,
378 UnicodeString& appendTo) const;
379
390 UnicodeString& format( int32_t number,
391 UnicodeString& appendTo) const;
392
403 UnicodeString& format( int64_t number,
404 UnicodeString& appendTo) const;
405
418 virtual UnicodeString& format(double number,
419 UnicodeString& appendTo,
420 FieldPosition& pos) const = 0;
435 virtual UnicodeString& format(double number,
436 UnicodeString& appendTo,
437 FieldPosition& pos,
438 UErrorCode &status) const;
453 virtual UnicodeString& format(double number,
454 UnicodeString& appendTo,
455 FieldPositionIterator* posIter,
456 UErrorCode& status) const;
469 virtual UnicodeString& format(int32_t number,
470 UnicodeString& appendTo,
471 FieldPosition& pos) const = 0;
472
486 virtual UnicodeString& format(int32_t number,
487 UnicodeString& appendTo,
488 FieldPosition& pos,
489 UErrorCode &status) const;
490
505 virtual UnicodeString& format(int32_t number,
506 UnicodeString& appendTo,
507 FieldPositionIterator* posIter,
508 UErrorCode& status) const;
522 virtual UnicodeString& format(int64_t number,
523 UnicodeString& appendTo,
524 FieldPosition& pos) const;
525
540 virtual UnicodeString& format(int64_t number,
541 UnicodeString& appendTo,
542 FieldPosition& pos,
543 UErrorCode& status) const;
558 virtual UnicodeString& format(int64_t number,
559 UnicodeString& appendTo,
560 FieldPositionIterator* posIter,
561 UErrorCode& status) const;
562
580 UnicodeString& appendTo,
581 FieldPositionIterator* posIter,
582 UErrorCode& status) const;
583
584// Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods
585
603 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
604 UnicodeString& appendTo,
605 FieldPositionIterator* posIter,
606 UErrorCode& status) const;
607
625 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
626 UnicodeString& appendTo,
627 FieldPosition& pos,
628 UErrorCode& status) const;
629
649 virtual void parse(const UnicodeString& text,
650 Formattable& result,
651 ParsePosition& parsePosition) const = 0;
652
668 virtual void parse(const UnicodeString& text,
669 Formattable& result,
670 UErrorCode& status) const;
671
692 ParsePosition& pos) const;
693
706
714 virtual void setParseIntegerOnly(UBool value);
715
723 virtual void setLenient(UBool enable);
724
733 virtual UBool isLenient() const;
734
744
754 static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
755 UErrorCode&);
756
768 static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
769 UNumberFormatStyle style,
770 UErrorCode& errorCode);
771
772#ifndef U_HIDE_INTERNAL_API
773
780 const Locale& desiredLocale,
781 UNumberFormatStyle style,
782 UErrorCode& errorCode);
783
791 static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
792 const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
793
794#endif /* U_HIDE_INTERNAL_API */
795
804
813 static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
814 UErrorCode&);
815
824
833 static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
834 UErrorCode&);
835
844
853 static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
854 UErrorCode&);
855
861 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
862
863#if !UCONFIG_NO_SERVICE
874 static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
875
888 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
889
897#endif /* UCONFIG_NO_SERVICE */
898
909
916 virtual void setGroupingUsed(UBool newValue);
917
926 int32_t getMaximumIntegerDigits() const;
927
940 virtual void setMaximumIntegerDigits(int32_t newValue);
941
950 int32_t getMinimumIntegerDigits() const;
951
962 virtual void setMinimumIntegerDigits(int32_t newValue);
963
973
984 virtual void setMaximumFractionDigits(int32_t newValue);
985
995
1006 virtual void setMinimumFractionDigits(int32_t newValue);
1007
1020 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec);
1021
1029 const char16_t* getCurrency() const;
1030
1040 virtual void setContext(UDisplayContext value, UErrorCode& status);
1041
1053
1061
1067 virtual void setRoundingMode(ERoundingMode roundingMode);
1068
1069public:
1070
1079 static UClassID U_EXPORT2 getStaticClassID();
1080
1092 virtual UClassID getDynamicClassID() const override = 0;
1093
1094protected:
1095
1101
1107
1113
1123 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const;
1124
1125#ifndef U_HIDE_INTERNAL_API
1132 static NumberFormat* makeInstance(const Locale& desiredLocale,
1133 UNumberFormatStyle style,
1134 UBool mustBeDecimalFormat,
1135 UErrorCode& errorCode);
1136#endif /* U_HIDE_INTERNAL_API */
1137
1138private:
1139
1140 static UBool isStyleSupported(UNumberFormatStyle style);
1141
1149 static NumberFormat* makeInstance(const Locale& desiredLocale,
1150 UNumberFormatStyle style,
1151 UErrorCode& errorCode);
1152
1153 UBool fGroupingUsed;
1154 int32_t fMaxIntegerDigits;
1155 int32_t fMinIntegerDigits;
1156 int32_t fMaxFractionDigits;
1157 int32_t fMinFractionDigits;
1158
1159 protected:
1161 static const int32_t gDefaultMaxIntegerDigits;
1163 static const int32_t gDefaultMinIntegerDigits;
1164
1165 private:
1166 UBool fParseIntegerOnly;
1167 UBool fLenient; // true => lenient parse is enabled
1168
1169 // ISO currency code
1170 char16_t fCurrency[4];
1171
1172 UDisplayContext fCapitalizationContext;
1173
1174 friend class ICUNumberFormatFactory; // access to makeInstance
1175 friend class ICUNumberFormatService;
1176 friend class ::NumberFormatTest; // access to isStyleSupported()
1177};
1178
1179#if !UCONFIG_NO_SERVICE
1189public:
1190
1196
1203 virtual UBool visible() const = 0;
1204
1210 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
1211
1219 virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
1220};
1221
1227protected:
1233
1239
1240public:
1245
1250
1254 virtual UBool visible() const override;
1255
1259 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const override;
1260};
1261#endif /* #if !UCONFIG_NO_SERVICE */
1262
1263// -------------------------------------
1264
1265inline UBool
1267{
1268 return fParseIntegerOnly;
1269}
1270
1271inline UBool
1273{
1274 return fLenient;
1275}
1276
1277U_NAMESPACE_END
1278
1279#endif /* #if !UCONFIG_NO_FORMATTING */
1280
1281#endif /* U_SHOW_CPLUSPLUS_API */
1282
1283#endif // _NUMFMT
1284//eof
A currency together with a numeric amount, such as 200 USD.
Definition curramt.h:39
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition fpositer.h:58
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:110
U_I18N_API Format()
Default constructor for subclass use only.
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:63
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:198
A NumberFormatFactory is used to register new number formats.
Definition numfmt.h:1188
virtual NumberFormat * createFormat(const Locale &loc, UNumberFormatStyle formatType)=0
Return a number format of the appropriate type.
virtual UBool visible() const =0
Return true if this factory will be visible.
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const =0
Return the locale names directly supported by this factory.
virtual ~NumberFormatFactory()
Destructor.
virtual void getEffectiveCurrency(char16_t *result, UErrorCode &ec) const
Returns the currency in effect for this formatter.
virtual void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a double number.
virtual void setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
virtual void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode &status) const
Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYP...
static const Locale * getAvailableLocales(int32_t &count)
Get the set of Locales for which NumberFormats are installed.
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
UnicodeString & format(int64_t number, UnicodeString &appendTo) const
Format an int64 number.
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
static NumberFormat * createCurrencyInstance(UErrorCode &)
Returns a currency format for the current default locale.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
Parse a string to produce an object.
int32_t getMinimumFractionDigits() const
Returns the minimum number of digits allowed in the fraction portion of a number.
virtual NumberFormat * clone() const override=0
Clones this object polymorphically.
static NumberFormat * createPercentInstance(const Locale &inLocale, UErrorCode &)
Returns a percentage format for the specified locale.
static const int32_t gDefaultMaxIntegerDigits
Definition numfmt.h:1161
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
const char16_t * getCurrency() const
Gets the currency used to display currency amounts.
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
static NumberFormat * makeInstance(const Locale &desiredLocale, UNumberFormatStyle style, UBool mustBeDecimalFormat, UErrorCode &errorCode)
Creates the specified number format style of the desired locale.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format an object to produce a string.
virtual ~NumberFormat()
Destructor.
static NumberFormat * internalCreateInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
ICU use only.
virtual void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format an int32 number.
UnicodeString & format(double number, UnicodeString &appendTo) const
Format a double number.
NumberFormat()
Default constructor for subclass use only.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos) const =0
Format a long number.
static const int32_t gDefaultMinIntegerDigits
Definition numfmt.h:1163
static NumberFormat * createScientificInstance(UErrorCode &)
Returns a scientific format for the current default locale.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an int64 number.
EAlignmentFields
Alignment Field constants used to construct a FieldPosition object.
Definition numfmt.h:234
@ INTEGER_FIELD
These constants are provided for backwards compatibility only.
Definition numfmt.h:267
@ kGroupingSeparatorField
Definition numfmt.h:248
static NumberFormat * createInstance(UErrorCode &)
Create a default style NumberFormat for the current default locale.
int32_t getMaximumIntegerDigits() const
Returns the maximum number of digits allowed in the integer portion of a number.
virtual void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
static URegistryKey registerFactory(NumberFormatFactory *toAdopt, UErrorCode &status)
Register a new NumberFormatFactory.
static StringEnumeration * getAvailableLocales()
Return a StringEnumeration over the locales available at the time of the call, including registered l...
static UClassID getStaticClassID()
Return the class ID for this class.
virtual void setCurrency(const char16_t *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
virtual void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a decimal number.
virtual UnicodeString & format(StringPiece number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
virtual ERoundingMode getRoundingMode() const
Get the rounding mode.
ERoundingMode
Rounding mode.
Definition numfmt.h:186
@ kRoundDown
Round towards zero.
Definition numfmt.h:189
@ kRoundHalfDown
Round towards the nearest integer, or towards zero if equidistant.
Definition numfmt.h:193
@ kRoundFloor
Round towards negative infinity.
Definition numfmt.h:188
@ kRoundCeiling
Round towards positive infinity.
Definition numfmt.h:187
@ kRoundHalfEven
Round towards the nearest integer, or towards the nearest even integer if equidistant.
Definition numfmt.h:191
@ kRoundUnnecessary
Return U_FORMAT_INEXACT_ERROR if number does not format exactly.
Definition numfmt.h:201
@ kRoundHalfFloor
Rounds ties toward -∞.
Definition numfmt.h:216
@ kRoundHalfOdd
Rounds ties toward the odd number.
Definition numfmt.h:206
@ kRoundHalfCeiling
Rounds ties toward +∞.
Definition numfmt.h:211
@ kRoundUp
Round away from zero.
Definition numfmt.h:190
@ kRoundHalfUp
Round towards the nearest integer, or away from zero if equidistant.
Definition numfmt.h:195
UnicodeString & format(int32_t number, UnicodeString &appendTo) const
Format a long number.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos) const
Format an int64 number.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos) const =0
Format a double number.
static NumberFormat * createScientificInstance(const Locale &inLocale, UErrorCode &)
Returns a scientific format for the specified locale.
static NumberFormat * createInstance(const Locale &inLocale, UErrorCode &)
Create a default style NumberFormat for the specified locale.
static NumberFormat * createCurrencyInstance(const Locale &inLocale, UErrorCode &)
Returns a currency format for the specified locale.
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
UBool isGroupingUsed() const
Returns true if grouping is used in this format.
virtual UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a long number.
virtual UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format an int64 number.
static UBool unregister(URegistryKey key, UErrorCode &status)
Unregister a previously-registered NumberFormatFactory using the key returned from the register call.
virtual UClassID getDynamicClassID() const override=0
Returns a unique class ID POLYMORPHICALLY.
static NumberFormat * createInstance(const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
Create a specific style NumberFormat for the specified locale.
int32_t getMaximumFractionDigits() const
Returns the maximum number of digits allowed in the fraction portion of a number.
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
static NumberFormat * createPercentInstance(UErrorCode &)
Returns a percentage format for the current default locale.
static const SharedNumberFormat * createSharedInstance(const Locale &inLocale, UNumberFormatStyle style, UErrorCode &status)
ICU use only.
virtual UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a double number.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format an object to produce a string.
virtual CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const
Parses text from the given string as a currency amount.
virtual UBool isLenient() const
Returns whether lenient parsing is enabled (it is off by default).
Definition numfmt.h:1272
NumberFormat(const NumberFormat &)
Copy constructor.
int32_t getMinimumIntegerDigits() const
Returns the minimum number of digits allowed in the integer portion of a number.
virtual void parse(const UnicodeString &text, Formattable &result, UErrorCode &status) const
Parse a string as a numeric value, and return a Formattable numeric object.
UBool isParseIntegerOnly() const
Return true if this format will parse numbers as integers only.
Definition numfmt.h:1266
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:52
SimpleNumberFormatFactory(const Locale &locale, UBool visible=true)
virtual UBool visible() const override
virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode &status) const override
UnicodeString _id
The locale supported by this factory, as a UnicodeString.
Definition numfmt.h:1238
const UBool _visible
True if the locale supported by this factory is visible.
Definition numfmt.h:1232
Base class for 'pure' C++ implementations of uenum api.
Definition strenum.h:61
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
C++ API: Currency Amount Object.
C++ API: Base class for all formats.
C++ API: Locale ID object.
C++ API: StringPiece: Read-only byte string wrapper class.
C API: Display context types (enum values).
UDisplayContextType
Display context types, for getting values of a particular setting.
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
const void * URegistryKey
Opaque type returned by registerInstance, registerFactory and unregister for service registration.
Definition umisc.h:57
C++ API: Unicode String.
C API: Compatibility APIs for number formatting.
UNumberFormatStyle
The possible number format styles.
Definition unum.h:150
@ UNUM_MEASURE_UNIT_FIELD
Definition unum.h:351
@ UNUM_COMPACT_FIELD
Definition unum.h:353
@ UNUM_INTEGER_FIELD
Definition unum.h:329
@ UNUM_PERCENT_FIELD
Definition unum.h:345
@ UNUM_PERMILL_FIELD
Definition unum.h:347
@ UNUM_EXPONENT_SYMBOL_FIELD
Definition unum.h:335
@ UNUM_GROUPING_SEPARATOR_FIELD
Definition unum.h:341
@ UNUM_DECIMAL_SEPARATOR_FIELD
Definition unum.h:333
@ UNUM_SIGN_FIELD
Definition unum.h:349
@ UNUM_CURRENCY_FIELD
Definition unum.h:343
@ UNUM_FRACTION_FIELD
Definition unum.h:331
@ UNUM_EXPONENT_SIGN_FIELD
Definition unum.h:337
@ UNUM_EXPONENT_FIELD
Definition unum.h:339
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316