ICU 64.2  64.2
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 
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 
666  public:
672  kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
673  };
674 
691  DecimalFormat(UErrorCode& status);
692 
710  DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
711 
733  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
734 
735 #ifndef U_HIDE_INTERNAL_API
736 
749  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
750  UNumberFormatStyle style, UErrorCode& status);
751 
752 #if UCONFIG_HAVE_PARSEALLINPUT
753 
757  void setParseAllInput(UNumberFormatAttributeValue value);
758 
759 #endif
760 
761 #endif /* U_HIDE_INTERNAL_API */
762 
763  private:
764 
769  DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
770 
771  public:
772 
783  virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
784 
794  virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
795 
796 
803  void setGroupingUsed(UBool newValue) U_OVERRIDE;
804 
812  void setParseIntegerOnly(UBool value) U_OVERRIDE;
813 
821  void setLenient(UBool enable) U_OVERRIDE;
822 
845  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
846  UParseError& parseError, UErrorCode& status);
847 
868  DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
869 
876  DecimalFormat(const DecimalFormat& source);
877 
884  DecimalFormat& operator=(const DecimalFormat& rhs);
885 
891 
899  Format* clone(void) const U_OVERRIDE;
900 
909  UBool operator==(const Format& other) const U_OVERRIDE;
910 
911 
912  using NumberFormat::format;
913 
925  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
926 
927 #ifndef U_HIDE_INTERNAL_API
928 
940  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
941  UErrorCode& status) const U_OVERRIDE;
942 #endif /* U_HIDE_INTERNAL_API */
943 
957  UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
958  UErrorCode& status) const U_OVERRIDE;
959 
971  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
972 
973 #ifndef U_HIDE_INTERNAL_API
974 
986  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
987  UErrorCode& status) const U_OVERRIDE;
988 #endif /* U_HIDE_INTERNAL_API */
989 
1003  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1004  UErrorCode& status) const U_OVERRIDE;
1005 
1017  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
1018 
1019 #ifndef U_HIDE_INTERNAL_API
1020 
1032  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1033  UErrorCode& status) const U_OVERRIDE;
1034 #endif /* U_HIDE_INTERNAL_API */
1035 
1049  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1050  UErrorCode& status) const U_OVERRIDE;
1051 
1068  UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1069  UErrorCode& status) const U_OVERRIDE;
1070 
1071 #ifndef U_HIDE_INTERNAL_API
1072 
1088  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1089  FieldPositionIterator* posIter, UErrorCode& status) const U_OVERRIDE;
1090 
1106  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1107  FieldPosition& pos, UErrorCode& status) const U_OVERRIDE;
1108 
1109 #endif // U_HIDE_INTERNAL_API
1110 
1111  using NumberFormat::parse;
1112 
1132  void parse(const UnicodeString& text, Formattable& result,
1133  ParsePosition& parsePosition) const U_OVERRIDE;
1134 
1154  CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const U_OVERRIDE;
1155 
1163  virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
1164 
1171  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1172 
1179  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1180 
1181 
1188  virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1189 
1196  virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
1197 
1204  virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
1205 
1206 
1215  UnicodeString& getPositivePrefix(UnicodeString& result) const;
1216 
1224  virtual void setPositivePrefix(const UnicodeString& newValue);
1225 
1234  UnicodeString& getNegativePrefix(UnicodeString& result) const;
1235 
1243  virtual void setNegativePrefix(const UnicodeString& newValue);
1244 
1253  UnicodeString& getPositiveSuffix(UnicodeString& result) const;
1254 
1262  virtual void setPositiveSuffix(const UnicodeString& newValue);
1263 
1272  UnicodeString& getNegativeSuffix(UnicodeString& result) const;
1273 
1281  virtual void setNegativeSuffix(const UnicodeString& newValue);
1282 
1283 #ifndef U_HIDE_DRAFT_API
1284 
1292  UBool isSignAlwaysShown() const;
1293 
1302  void setSignAlwaysShown(UBool value);
1303 #endif /* U_HIDE_DRAFT_API */
1304 
1317  int32_t getMultiplier(void) const;
1318 
1332  virtual void setMultiplier(int32_t newValue);
1333 
1334 #ifndef U_HIDE_DRAFT_API
1335 
1347  int32_t getMultiplierScale(void) const;
1348 
1368  void setMultiplierScale(int32_t newValue);
1369 #endif /* U_HIDE_DRAFT_API */
1370 
1380  virtual double getRoundingIncrement(void) const;
1381 
1393  virtual void setRoundingIncrement(double newValue);
1394 
1403  virtual ERoundingMode getRoundingMode(void) const U_OVERRIDE;
1404 
1413  virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1414 
1426  virtual int32_t getFormatWidth(void) const;
1427 
1442  virtual void setFormatWidth(int32_t width);
1443 
1456  virtual UnicodeString getPadCharacterString() const;
1457 
1472  virtual void setPadCharacter(const UnicodeString& padChar);
1473 
1489  virtual EPadPosition getPadPosition(void) const;
1490 
1507  virtual void setPadPosition(EPadPosition padPos);
1508 
1519  virtual UBool isScientificNotation(void) const;
1520 
1536  virtual void setScientificNotation(UBool useScientific);
1537 
1548  virtual int8_t getMinimumExponentDigits(void) const;
1549 
1562  virtual void setMinimumExponentDigits(int8_t minExpDig);
1563 
1576  virtual UBool isExponentSignAlwaysShown(void) const;
1577 
1591  virtual void setExponentSignAlwaysShown(UBool expSignAlways);
1592 
1604  int32_t getGroupingSize(void) const;
1605 
1617  virtual void setGroupingSize(int32_t newValue);
1618 
1637  int32_t getSecondaryGroupingSize(void) const;
1638 
1650  virtual void setSecondaryGroupingSize(int32_t newValue);
1651 
1652 #ifndef U_HIDE_DRAFT_API
1653 
1676  int32_t getMinimumGroupingDigits() const;
1677 
1688  void setMinimumGroupingDigits(int32_t newValue);
1689 #endif /* U_HIDE_DRAFT_API */
1690 
1691 
1700  UBool isDecimalSeparatorAlwaysShown(void) const;
1701 
1710  virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
1711 
1718  UBool isDecimalPatternMatchRequired(void) const;
1719 
1729  virtual void setDecimalPatternMatchRequired(UBool newValue);
1730 
1731 #ifndef U_HIDE_DRAFT_API
1732 
1739  UBool isParseNoExponent() const;
1740 
1749  void setParseNoExponent(UBool value);
1750 
1758  UBool isParseCaseSensitive() const;
1759 
1771  void setParseCaseSensitive(UBool value);
1772 
1781  UBool isFormatFailIfMoreThanMaxDigits() const;
1782 
1790  void setFormatFailIfMoreThanMaxDigits(UBool value);
1791 #endif /* U_HIDE_DRAFT_API */
1792 
1793 
1804  virtual UnicodeString& toPattern(UnicodeString& result) const;
1805 
1816  virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
1817 
1847  virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
1848 
1857  virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1858 
1889  virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
1890  UErrorCode& status);
1891 
1901  virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
1902 
1903 
1913  void setMaximumIntegerDigits(int32_t newValue) U_OVERRIDE;
1914 
1924  void setMinimumIntegerDigits(int32_t newValue) U_OVERRIDE;
1925 
1935  void setMaximumFractionDigits(int32_t newValue) U_OVERRIDE;
1936 
1946  void setMinimumFractionDigits(int32_t newValue) U_OVERRIDE;
1947 
1955  int32_t getMinimumSignificantDigits() const;
1956 
1964  int32_t getMaximumSignificantDigits() const;
1965 
1977  void setMinimumSignificantDigits(int32_t min);
1978 
1990  void setMaximumSignificantDigits(int32_t max);
1991 
1998  UBool areSignificantDigitsUsed() const;
1999 
2007  void setSignificantDigitsUsed(UBool useSignificantDigits);
2008 
2021  void setCurrency(const char16_t* theCurrency, UErrorCode& ec) U_OVERRIDE;
2022 
2028  virtual void setCurrency(const char16_t* theCurrency);
2029 
2038  void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec);
2039 
2044  UCurrencyUsage getCurrencyUsage() const;
2045 
2046 #ifndef U_HIDE_INTERNAL_API
2047 
2053  void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2054  UErrorCode& status) const;
2055 
2062  void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2063  UErrorCode& status) const;
2064 
2065 #endif /* U_HIDE_INTERNAL_API */
2066 
2067 #ifndef U_HIDE_DRAFT_API
2068 
2113  const number::LocalizedNumberFormatter* toNumberFormatter(UErrorCode& status) const;
2114 #endif /* U_HIDE_DRAFT_API */
2115 
2116 #ifndef U_HIDE_DEPRECATED_API
2117 
2129  const number::LocalizedNumberFormatter& toNumberFormatter() const;
2130 #endif /* U_HIDE_DEPRECATED_API */
2131 
2143  static UClassID U_EXPORT2 getStaticClassID(void);
2144 
2156  UClassID getDynamicClassID(void) const U_OVERRIDE;
2157 
2158  private:
2159 
2161  void touch(UErrorCode& status);
2162 
2164  void touchNoError();
2165 
2177  void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2178  UErrorCode& status);
2179 
2180  const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2181 
2182  const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2183 
2184  static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2185  int32_t offset, UErrorCode& status);
2186 
2187  static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2188  FieldPositionIterator* fpi, int32_t offset, UErrorCode& status);
2189 
2190  void setupFastFormat();
2191 
2192  bool fastFormatDouble(double input, UnicodeString& output) const;
2193 
2194  bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2195 
2196  void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2197 
2198  //=====================================================================================//
2199  // INSTANCE FIELDS //
2200  //=====================================================================================//
2201 
2202 
2203  // One instance field for the implementation, keep all fields inside of an implementation
2204  // class defined in number_mapper.h
2205  number::impl::DecimalFormatFields* fields = nullptr;
2206 
2207  // Allow child class CompactDecimalFormat to access fProperties:
2208  friend class CompactDecimalFormat;
2209 
2210  // Allow MeasureFormat to use fieldPositionHelper:
2211  friend class MeasureFormat;
2212 
2213 };
2214 
2216 
2217 #endif /* #if !UCONFIG_NO_FORMATTING */
2218 
2219 #endif // _DECIMFMT
2220 //eof
Base class for all formats.
Definition: format.h:96
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:84
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: decimfmt.h:665
C++ API: FieldPosition Iterator.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
C++ API: StringPiece: Read-only byte string wrapper class.
C++ API: Currency Amount Object.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:146
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
EPadPosition
Pad position.
Definition: decimfmt.h:671
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
ERoundingMode
Rounding mode.
Definition: numfmt.h:184
C++ API: Symbols for formatting numbers.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
C++ API: Locale ID object.
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
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
Basic definitions for ICU, for both C and C++ APIs.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
The result of a number formatting operation.
C++: internal template EnumSet<>
C++ API: Compatibility APIs for number formatting.
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition: unum.h:967
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:37
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225