ICU 63.1 63.1

decimfmt.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
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
8 *
9 * File DECIMFMT.H
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/20/97 clhuang Updated per C++ implementation.
16 * 04/03/97 aliu Rewrote parsing and formatting completely, and
17 * cleaned up and debugged. Actually works now.
18 * 04/17/97 aliu Changed DigitCount to int per code review.
19 * 07/10/97 helena Made ParsePosition a class and get rid of the function
20 * hiding problems.
21 * 09/09/97 aliu Ported over support for exponential formats.
22 * 07/20/98 stephen Changed documentation
23 * 01/30/13 emmons Added Scaling methods
24 ********************************************************************************
25 */
26
27 #ifndef DECIMFMT_H
28 #define DECIMFMT_H
29
30 #include "unicode/utypes.h"
36 #if !UCONFIG_NO_FORMATTING
37
38 #include "unicode/dcfmtsym.h"
39 #include "unicode/numfmt.h"
40 #include "unicode/locid.h"
41 #include "unicode/fpositer.h"
42 #include "unicode/stringpiece.h"
43 #include "unicode/curramt.h"
44 #include "unicode/enumset.h"
45
46 U_NAMESPACE_BEGIN
47
48 class CurrencyPluralInfo;
49 class CompactDecimalFormat;
50
51 namespace number {
52 class LocalizedNumberFormatter;
53 class FormattedNumber;
54 namespace impl {
55 class DecimalQuantity;
56 struct DecimalFormatFields;
57 }
58 }
59
60 namespace numparse {
61 namespace impl {
62 class NumberParserImpl;
63 }
64 }
65
71 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
72 template class U_I18N_API EnumSet<UNumberFormatAttribute,
73 UNUM_MAX_NONBOOLEAN_ATTRIBUTE+1,
74 UNUM_LIMIT_BOOLEAN_ATTRIBUTE>;
75 #endif
76
677 class U_I18N_API DecimalFormat : public NumberFormat {
678 public:
683 enum EPadPosition {
684 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
685 };
686
703 DecimalFormat(UErrorCode& status);
704
722 DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
723
745 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
746
747 #ifndef U_HIDE_INTERNAL_API
748
761 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
762 UNumberFormatStyle style, UErrorCode& status);
763
764 #if UCONFIG_HAVE_PARSEALLINPUT
765
769 void setParseAllInput(UNumberFormatAttributeValue value);
770
771 #endif
772
773 #endif /* U_HIDE_INTERNAL_API */
774
775 private:
776
781 DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
782
783 public:
784
795 virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
796
806 virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
807
808
815 void setGroupingUsed(UBool newValue) U_OVERRIDE;
816
824 void setParseIntegerOnly(UBool value) U_OVERRIDE;
825
833 void setLenient(UBool enable) U_OVERRIDE;
834
857 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
858 UParseError& parseError, UErrorCode& status);
859
880 DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
881
888 DecimalFormat(const DecimalFormat& source);
889
896 DecimalFormat& operator=(const DecimalFormat& rhs);
897
902 ~DecimalFormat() U_OVERRIDE;
903
911 Format* clone(void) const U_OVERRIDE;
912
921 UBool operator==(const Format& other) const U_OVERRIDE;
922
923
924 using NumberFormat::format;
925
937 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
938
939 #ifndef U_HIDE_INTERNAL_API
940
952 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
953 UErrorCode& status) const U_OVERRIDE;
954 #endif /* U_HIDE_INTERNAL_API */
955
969 UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
970 UErrorCode& status) const U_OVERRIDE;
971
983 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
984
985 #ifndef U_HIDE_INTERNAL_API
986
998 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
999 UErrorCode& status) const U_OVERRIDE;
1000 #endif /* U_HIDE_INTERNAL_API */
1001
1015 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1016 UErrorCode& status) const U_OVERRIDE;
1017
1029 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
1030
1031 #ifndef U_HIDE_INTERNAL_API
1032
1044 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1045 UErrorCode& status) const U_OVERRIDE;
1046 #endif /* U_HIDE_INTERNAL_API */
1047
1061 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1062 UErrorCode& status) const U_OVERRIDE;
1063
1080 UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1081 UErrorCode& status) const U_OVERRIDE;
1082
1083 #ifndef U_HIDE_INTERNAL_API
1084
1100 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1101 FieldPositionIterator* posIter, UErrorCode& status) const U_OVERRIDE;
1102
1118 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1119 FieldPosition& pos, UErrorCode& status) const U_OVERRIDE;
1120
1121 #endif // U_HIDE_INTERNAL_API
1122
1123 using NumberFormat::parse;
1124
1144 void parse(const UnicodeString& text, Formattable& result,
1145 ParsePosition& parsePosition) const U_OVERRIDE;
1146
1166 CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const U_OVERRIDE;
1167
1175 virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
1176
1183 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1184
1191 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1192
1193
1200 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1201
1208 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
1209
1216 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
1217
1218
1227 UnicodeString& getPositivePrefix(UnicodeString& result) const;
1228
1236 virtual void setPositivePrefix(const UnicodeString& newValue);
1237
1246 UnicodeString& getNegativePrefix(UnicodeString& result) const;
1247
1255 virtual void setNegativePrefix(const UnicodeString& newValue);
1256
1265 UnicodeString& getPositiveSuffix(UnicodeString& result) const;
1266
1274 virtual void setPositiveSuffix(const UnicodeString& newValue);
1275
1284 UnicodeString& getNegativeSuffix(UnicodeString& result) const;
1285
1293 virtual void setNegativeSuffix(const UnicodeString& newValue);
1294
1295 #ifndef U_HIDE_INTERNAL_API
1296
1300 UBool isSignAlwaysShown() const;
1301 #endif /* U_HIDE_INTERNAL_API */
1302
1308 virtual void setSignAlwaysShown(UBool value);
1309
1322 int32_t getMultiplier(void) const;
1323
1337 virtual void setMultiplier(int32_t newValue);
1338
1339 #ifndef U_HIDE_DRAFT_API
1340
1352 int32_t getMultiplierScale(void) const;
1353 #endif /* U_HIDE_DRAFT_API */
1354
1374 virtual void setMultiplierScale(int32_t newValue);
1375
1385 virtual double getRoundingIncrement(void) const;
1386
1398 virtual void setRoundingIncrement(double newValue);
1399
1408 virtual ERoundingMode getRoundingMode(void) const U_OVERRIDE;
1409
1418 virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1419
1431 virtual int32_t getFormatWidth(void) const;
1432
1447 virtual void setFormatWidth(int32_t width);
1448
1461 virtual UnicodeString getPadCharacterString() const;
1462
1477 virtual void setPadCharacter(const UnicodeString& padChar);
1478
1494 virtual EPadPosition getPadPosition(void) const;
1495
1512 virtual void setPadPosition(EPadPosition padPos);
1513
1524 virtual UBool isScientificNotation(void) const;
1525
1541 virtual void setScientificNotation(UBool useScientific);
1542
1553 virtual int8_t getMinimumExponentDigits(void) const;
1554
1567 virtual void setMinimumExponentDigits(int8_t minExpDig);
1568
1581 virtual UBool isExponentSignAlwaysShown(void) const;
1582
1596 virtual void setExponentSignAlwaysShown(UBool expSignAlways);
1597
1609 int32_t getGroupingSize(void) const;
1610
1622 virtual void setGroupingSize(int32_t newValue);
1623
1642 int32_t getSecondaryGroupingSize(void) const;
1643
1655 virtual void setSecondaryGroupingSize(int32_t newValue);
1656
1657 #ifndef U_HIDE_INTERNAL_API
1658
1680 int32_t getMinimumGroupingDigits() const;
1681
1682 #endif /* U_HIDE_INTERNAL_API */
1683
1684 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual. */
1693 virtual void setMinimumGroupingDigits(int32_t newValue);
1694
1695
1704 UBool isDecimalSeparatorAlwaysShown(void) const;
1705
1714 virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
1715
1722 UBool isDecimalPatternMatchRequired(void) const;
1723
1733 virtual void setDecimalPatternMatchRequired(UBool newValue);
1734
1741 virtual UBool isParseNoExponent() const;
1742
1751 virtual void setParseNoExponent(UBool value);
1752
1759 virtual UBool isParseCaseSensitive() const;
1760
1770 virtual void setParseCaseSensitive(UBool value);
1771
1779 virtual UBool isFormatFailIfMoreThanMaxDigits() const;
1780
1787 virtual void setFormatFailIfMoreThanMaxDigits(UBool value);
1788
1789
1800 virtual UnicodeString& toPattern(UnicodeString& result) const;
1801
1812 virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
1813
1843 virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
1844
1853 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1854
1885 virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
1886 UErrorCode& status);
1887
1897 virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
1898
1899
1909 void setMaximumIntegerDigits(int32_t newValue) U_OVERRIDE;
1910
1920 void setMinimumIntegerDigits(int32_t newValue) U_OVERRIDE;
1921
1931 void setMaximumFractionDigits(int32_t newValue) U_OVERRIDE;
1932
1942 void setMinimumFractionDigits(int32_t newValue) U_OVERRIDE;
1943
1951 int32_t getMinimumSignificantDigits() const;
1952
1960 int32_t getMaximumSignificantDigits() const;
1961
1973 void setMinimumSignificantDigits(int32_t min);
1974
1986 void setMaximumSignificantDigits(int32_t max);
1987
1994 UBool areSignificantDigitsUsed() const;
1995
2003 void setSignificantDigitsUsed(UBool useSignificantDigits);
2004
2017 void setCurrency(const char16_t* theCurrency, UErrorCode& ec) U_OVERRIDE;
2018
2024 virtual void setCurrency(const char16_t* theCurrency);
2025
2034 void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec);
2035
2040 UCurrencyUsage getCurrencyUsage() const;
2041
2042 #ifndef U_HIDE_INTERNAL_API
2043
2049 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2050 UErrorCode& status) const;
2051
2058 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2059 UErrorCode& status) const;
2060
2061 #endif /* U_HIDE_INTERNAL_API */
2062
2063 #ifndef U_HIDE_DRAFT_API
2064
2086 const number::LocalizedNumberFormatter& toNumberFormatter() const;
2087 #endif /* U_HIDE_DRAFT_API */
2088
2100 static UClassID U_EXPORT2 getStaticClassID(void);
2101
2113 UClassID getDynamicClassID(void) const U_OVERRIDE;
2114
2115 private:
2116
2118 void touch(UErrorCode& status);
2119
2121 void touchNoError();
2122
2134 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2135 UErrorCode& status);
2136
2137 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2138
2139 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2140
2141 static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2142 int32_t offset, UErrorCode& status);
2143
2144 static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2145 FieldPositionIterator* fpi, int32_t offset, UErrorCode& status);
2146
2147 void setupFastFormat();
2148
2149 bool fastFormatDouble(double input, UnicodeString& output) const;
2150
2151 bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2152
2153 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2154
2155 //=====================================================================================//
2156 // INSTANCE FIELDS //
2157 //=====================================================================================//
2158
2159 // Only one instance field: keep all fields inside of an implementation class defined in number_mapper.h
2160 number::impl::DecimalFormatFields* fields;
2161
2162 // Allow child class CompactDecimalFormat to access fProperties:
2163 friend class CompactDecimalFormat;
2164
2165 };
2166
2167 U_NAMESPACE_END
2168
2169 #endif /* #if !UCONFIG_NO_FORMATTING */
2170
2171 #endif // _DECIMFMT
2172 //eof
icu::Format
Base class for all formats.
Definition: format.h:96
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:84
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
icu::NumberFormat::format
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an object to produce a string.
icu::NumberFormat::getDynamicClassID
virtual UClassID getDynamicClassID(void) const =0
Returns a unique class ID POLYMORPHICALLY.
icu::DecimalFormat
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: decimfmt.h:677
icu::NumberFormat
Definition: numfmt.h:173
icu::NumberFormat::parse
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
fpositer.h
C++ API: FieldPosition Iterator.
icu::NumberFormat::setMinimumIntegerDigits
virtual void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
icu::NumberFormat::setRoundingMode
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:90
UNUM_LIMIT_BOOLEAN_ATTRIBUTE
Limit of boolean attributes.
Definition: unum.h:1077
icu::NumberFormat::setLenient
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
icu::NumberFormat::setMinimumFractionDigits
virtual void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
icu::CompactDecimalFormat
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: compactdecimalformat.h:60
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.
icu::NumberFormat::parseCurrency
virtual CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const
Parses text from the given string as a currency amount.
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
UNumberFormatStyle
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
icu::NumberFormat::setGroupingUsed
virtual void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
UCurrencyUsage
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
icu::NumberFormat::setParseIntegerOnly
virtual void setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
icu::DecimalFormat::EPadPosition
EPadPosition
Pad position.
Definition: decimfmt.h:683
UNUM_MAX_NONBOOLEAN_ATTRIBUTE
One below the first bitfield-boolean item.
Definition: unum.h:1048
icu::NumberFormat::setMaximumIntegerDigits
virtual void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
icu::number::LocalizedNumberFormatter
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
Definition: numberformatter.h:2258
icu::NumberFormat::getRoundingMode
virtual ERoundingMode getRoundingMode(void) const
Get the rounding mode.
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
icu::NumberFormat::getStaticClassID
static UClassID getStaticClassID(void)
Return the class ID for this class.
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
dcfmtsym.h
C++ API: Symbols for formatting numbers.
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
icu::NumberFormat::setCurrency
virtual void setCurrency(const char16_t *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
locid.h
C++ API: Locale ID object.
icu::CurrencyPluralInfo
This class represents the information needed by DecimalFormat to format currency plural, such as "3.00 US dollars" or "1.00 US dollar".
Definition: currpinf.h:46
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
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
icu::number::FormattedNumber
The result of a number formatting operation.
Definition: numberformatter.h:2438
enumset.h
C++: internal template EnumSet<>
numfmt.h
C++ API: Compatibility APIs for number formatting.
UNumberFormatAttribute
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition: unum.h:960
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
icu::NumberFormat::setMaximumFractionDigits
virtual void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
icu::NumberFormat::operator=
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
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