ICU 65.1 65.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"
31
32 #if U_SHOW_CPLUSPLUS_API
33
39 #if !UCONFIG_NO_FORMATTING
40
41 #include "unicode/dcfmtsym.h"
42 #include "unicode/numfmt.h"
43 #include "unicode/locid.h"
44 #include "unicode/fpositer.h"
45 #include "unicode/stringpiece.h"
46 #include "unicode/curramt.h"
47 #include "unicode/enumset.h"
48
49 U_NAMESPACE_BEGIN
50
51 class CurrencyPluralInfo;
52 class CompactDecimalFormat;
53
54 namespace number {
55 class LocalizedNumberFormatter;
56 class FormattedNumber;
57 namespace impl {
58 class DecimalQuantity;
59 struct DecimalFormatFields;
60 }
61 }
62
63 namespace numparse {
64 namespace impl {
65 class NumberParserImpl;
66 }
67 }
68
668 class U_I18N_API DecimalFormat : public NumberFormat {
669 public:
674 enum EPadPosition {
675 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
676 };
677
694 DecimalFormat(UErrorCode& status);
695
713 DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
714
736 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
737
738 #ifndef U_HIDE_INTERNAL_API
739
752 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
753 UNumberFormatStyle style, UErrorCode& status);
754
755 #if UCONFIG_HAVE_PARSEALLINPUT
756
760 void setParseAllInput(UNumberFormatAttributeValue value);
761
762 #endif
763
764 #endif /* U_HIDE_INTERNAL_API */
765
766 private:
767
772 DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
773
774 public:
775
786 virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
787
797 virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
798
799
806 void setGroupingUsed(UBool newValue) U_OVERRIDE;
807
815 void setParseIntegerOnly(UBool value) U_OVERRIDE;
816
824 void setLenient(UBool enable) U_OVERRIDE;
825
848 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
849 UParseError& parseError, UErrorCode& status);
850
871 DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
872
879 DecimalFormat(const DecimalFormat& source);
880
887 DecimalFormat& operator=(const DecimalFormat& rhs);
888
893 ~DecimalFormat() U_OVERRIDE;
894
902 DecimalFormat* clone() const U_OVERRIDE;
903
912 UBool operator==(const Format& other) const U_OVERRIDE;
913
914
915 using NumberFormat::format;
916
928 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
929
930 #ifndef U_HIDE_INTERNAL_API
931
943 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
944 UErrorCode& status) const U_OVERRIDE;
945 #endif /* U_HIDE_INTERNAL_API */
946
960 UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
961 UErrorCode& status) const U_OVERRIDE;
962
974 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
975
976 #ifndef U_HIDE_INTERNAL_API
977
989 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
990 UErrorCode& status) const U_OVERRIDE;
991 #endif /* U_HIDE_INTERNAL_API */
992
1006 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1007 UErrorCode& status) const U_OVERRIDE;
1008
1020 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
1021
1022 #ifndef U_HIDE_INTERNAL_API
1023
1035 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1036 UErrorCode& status) const U_OVERRIDE;
1037 #endif /* U_HIDE_INTERNAL_API */
1038
1052 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1053 UErrorCode& status) const U_OVERRIDE;
1054
1071 UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1072 UErrorCode& status) const U_OVERRIDE;
1073
1074 #ifndef U_HIDE_INTERNAL_API
1075
1091 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1092 FieldPositionIterator* posIter, UErrorCode& status) const U_OVERRIDE;
1093
1109 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1110 FieldPosition& pos, UErrorCode& status) const U_OVERRIDE;
1111
1112 #endif // U_HIDE_INTERNAL_API
1113
1114 using NumberFormat::parse;
1115
1135 void parse(const UnicodeString& text, Formattable& result,
1136 ParsePosition& parsePosition) const U_OVERRIDE;
1137
1157 CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const U_OVERRIDE;
1158
1166 virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
1167
1174 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1175
1182 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1183
1184
1191 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1192
1199 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
1200
1207 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
1208
1209
1218 UnicodeString& getPositivePrefix(UnicodeString& result) const;
1219
1227 virtual void setPositivePrefix(const UnicodeString& newValue);
1228
1237 UnicodeString& getNegativePrefix(UnicodeString& result) const;
1238
1246 virtual void setNegativePrefix(const UnicodeString& newValue);
1247
1256 UnicodeString& getPositiveSuffix(UnicodeString& result) const;
1257
1265 virtual void setPositiveSuffix(const UnicodeString& newValue);
1266
1275 UnicodeString& getNegativeSuffix(UnicodeString& result) const;
1276
1284 virtual void setNegativeSuffix(const UnicodeString& newValue);
1285
1286 #ifndef U_HIDE_DRAFT_API
1287
1295 UBool isSignAlwaysShown() const;
1296
1305 void setSignAlwaysShown(UBool value);
1306 #endif /* U_HIDE_DRAFT_API */
1307
1320 int32_t getMultiplier(void) const;
1321
1335 virtual void setMultiplier(int32_t newValue);
1336
1349 int32_t getMultiplierScale(void) const;
1350
1370 void setMultiplierScale(int32_t newValue);
1371
1381 virtual double getRoundingIncrement(void) const;
1382
1394 virtual void setRoundingIncrement(double newValue);
1395
1404 virtual ERoundingMode getRoundingMode(void) const U_OVERRIDE;
1405
1414 virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1415
1427 virtual int32_t getFormatWidth(void) const;
1428
1443 virtual void setFormatWidth(int32_t width);
1444
1457 virtual UnicodeString getPadCharacterString() const;
1458
1473 virtual void setPadCharacter(const UnicodeString& padChar);
1474
1490 virtual EPadPosition getPadPosition(void) const;
1491
1508 virtual void setPadPosition(EPadPosition padPos);
1509
1520 virtual UBool isScientificNotation(void) const;
1521
1537 virtual void setScientificNotation(UBool useScientific);
1538
1549 virtual int8_t getMinimumExponentDigits(void) const;
1550
1563 virtual void setMinimumExponentDigits(int8_t minExpDig);
1564
1577 virtual UBool isExponentSignAlwaysShown(void) const;
1578
1592 virtual void setExponentSignAlwaysShown(UBool expSignAlways);
1593
1605 int32_t getGroupingSize(void) const;
1606
1618 virtual void setGroupingSize(int32_t newValue);
1619
1638 int32_t getSecondaryGroupingSize(void) const;
1639
1651 virtual void setSecondaryGroupingSize(int32_t newValue);
1652
1653 #ifndef U_HIDE_DRAFT_API
1654
1677 int32_t getMinimumGroupingDigits() const;
1678
1689 void setMinimumGroupingDigits(int32_t newValue);
1690 #endif /* U_HIDE_DRAFT_API */
1691
1692
1701 UBool isDecimalSeparatorAlwaysShown(void) const;
1702
1711 virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
1712
1719 UBool isDecimalPatternMatchRequired(void) const;
1720
1730 virtual void setDecimalPatternMatchRequired(UBool newValue);
1731
1732 #ifndef U_HIDE_DRAFT_API
1733
1740 UBool isParseNoExponent() const;
1741
1750 void setParseNoExponent(UBool value);
1751
1759 UBool isParseCaseSensitive() const;
1760
1772 void setParseCaseSensitive(UBool value);
1773
1782 UBool isFormatFailIfMoreThanMaxDigits() const;
1783
1791 void setFormatFailIfMoreThanMaxDigits(UBool value);
1792 #endif /* U_HIDE_DRAFT_API */
1793
1794
1805 virtual UnicodeString& toPattern(UnicodeString& result) const;
1806
1817 virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
1818
1848 virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
1849
1858 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1859
1890 virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
1891 UErrorCode& status);
1892
1902 virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
1903
1904
1914 void setMaximumIntegerDigits(int32_t newValue) U_OVERRIDE;
1915
1925 void setMinimumIntegerDigits(int32_t newValue) U_OVERRIDE;
1926
1936 void setMaximumFractionDigits(int32_t newValue) U_OVERRIDE;
1937
1947 void setMinimumFractionDigits(int32_t newValue) U_OVERRIDE;
1948
1956 int32_t getMinimumSignificantDigits() const;
1957
1965 int32_t getMaximumSignificantDigits() const;
1966
1978 void setMinimumSignificantDigits(int32_t min);
1979
1991 void setMaximumSignificantDigits(int32_t max);
1992
1999 UBool areSignificantDigitsUsed() const;
2000
2008 void setSignificantDigitsUsed(UBool useSignificantDigits);
2009
2022 void setCurrency(const char16_t* theCurrency, UErrorCode& ec) U_OVERRIDE;
2023
2024 #ifndef U_FORCE_HIDE_DEPRECATED_API
2025
2030 virtual void setCurrency(const char16_t* theCurrency);
2031 #endif // U_FORCE_HIDE_DEPRECATED_API
2032
2041 void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec);
2042
2047 UCurrencyUsage getCurrencyUsage() const;
2048
2049 #ifndef U_HIDE_INTERNAL_API
2050
2056 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2057 UErrorCode& status) const;
2058
2065 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2066 UErrorCode& status) const;
2067
2068 #endif /* U_HIDE_INTERNAL_API */
2069
2070 #ifndef U_HIDE_DRAFT_API
2071
2116 const number::LocalizedNumberFormatter* toNumberFormatter(UErrorCode& status) const;
2117 #endif /* U_HIDE_DRAFT_API */
2118
2130 static UClassID U_EXPORT2 getStaticClassID(void);
2131
2143 UClassID getDynamicClassID(void) const U_OVERRIDE;
2144
2145 private:
2146
2148 void touch(UErrorCode& status);
2149
2151 void touchNoError();
2152
2164 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2165 UErrorCode& status);
2166
2167 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2168
2169 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2170
2171 static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2172 int32_t offset, UErrorCode& status);
2173
2174 static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2175 FieldPositionIterator* fpi, int32_t offset, UErrorCode& status);
2176
2177 void setupFastFormat();
2178
2179 bool fastFormatDouble(double input, UnicodeString& output) const;
2180
2181 bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2182
2183 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2184
2185 //=====================================================================================//
2186 // INSTANCE FIELDS //
2187 //=====================================================================================//
2188
2189
2190 // One instance field for the implementation, keep all fields inside of an implementation
2191 // class defined in number_mapper.h
2192 number::impl::DecimalFormatFields* fields = nullptr;
2193
2194 // Allow child class CompactDecimalFormat to access fProperties:
2195 friend class CompactDecimalFormat;
2196
2197 // Allow MeasureFormat to use fieldPositionHelper:
2198 friend class MeasureFormat;
2199
2200 };
2201
2202 U_NAMESPACE_END
2203
2204 #endif /* #if !UCONFIG_NO_FORMATTING */
2205
2206 #endif /* U_SHOW_CPLUSPLUS_API */
2207
2208 #endif // _DECIMFMT
2209 //eof
icu::Format
Base class for all formats.
Definition: format.h:98
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
icu::DecimalFormat
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: decimfmt.h:668
icu::MeasureFormat
Definition: measfmt.h:100
icu::NumberFormat
Definition: numfmt.h:175
fpositer.h
C++ API: FieldPosition Iterator.
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
icu::CompactDecimalFormat
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: compactdecimalformat.h:63
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.
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:58
UCurrencyUsage
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
icu::DecimalFormat::EPadPosition
EPadPosition
Pad position.
Definition: decimfmt.h:674
icu::number::LocalizedNumberFormatter
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
Definition: numberformatter.h:2228
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
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:52
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:48
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:294
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::number::FormattedNumber
The result of a number formatting operation.
Definition: numberformatter.h:2410
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:969
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
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