ICU 62.1  62.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 
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 
66 // explicit template instantiation. see digitlst.h
67 // (When building DLLs for Windows this is required.)
68 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
69 template class U_I18N_API EnumSet<UNumberFormatAttribute,
72 #endif
73 
673  public:
679  kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
680  };
681 
698  DecimalFormat(UErrorCode& status);
699 
717  DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
718 
740  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
741 
742 #ifndef U_HIDE_INTERNAL_API
743 
756  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
757  UNumberFormatStyle style, UErrorCode& status);
758 
759 #if UCONFIG_HAVE_PARSEALLINPUT
760 
764  void setParseAllInput(UNumberFormatAttributeValue value);
765 
766 #endif
767 
768 #endif /* U_HIDE_INTERNAL_API */
769 
770  private:
771 
776  DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
777 
778  public:
779 
790  virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
791 
801  virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
802 
803 
810  void setGroupingUsed(UBool newValue) U_OVERRIDE;
811 
819  void setParseIntegerOnly(UBool value) U_OVERRIDE;
820 
828  void setLenient(UBool enable) U_OVERRIDE;
829 
852  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
853  UParseError& parseError, UErrorCode& status);
854 
875  DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
876 
883  DecimalFormat(const DecimalFormat& source);
884 
891  DecimalFormat& operator=(const DecimalFormat& rhs);
892 
897  ~DecimalFormat() U_OVERRIDE;
898 
906  Format* clone(void) const U_OVERRIDE;
907 
916  UBool operator==(const Format& other) const U_OVERRIDE;
917 
918 
919  using NumberFormat::format;
920 
932  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
933 
934 #ifndef U_HIDE_INTERNAL_API
935 
947  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
948  UErrorCode& status) const U_OVERRIDE;
949 #endif /* U_HIDE_INTERNAL_API */
950 
964  UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
965  UErrorCode& status) const U_OVERRIDE;
966 
978  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
979 
980 #ifndef U_HIDE_INTERNAL_API
981 
992  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
993  UErrorCode& status) const U_OVERRIDE;
994 #endif /* U_HIDE_INTERNAL_API */
995 
1009  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1010  UErrorCode& status) const U_OVERRIDE;
1011 
1023  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
1024 
1025 #ifndef U_HIDE_INTERNAL_API
1026 
1037  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1038  UErrorCode& status) const U_OVERRIDE;
1039 #endif /* U_HIDE_INTERNAL_API */
1040 
1054  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1055  UErrorCode& status) const U_OVERRIDE;
1056 
1073  UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1074  UErrorCode& status) const U_OVERRIDE;
1075 
1076 #ifndef U_HIDE_INTERNAL_API
1077 
1093  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1094  FieldPositionIterator* posIter, UErrorCode& status) const U_OVERRIDE;
1095 
1111  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1112  FieldPosition& pos, UErrorCode& status) const U_OVERRIDE;
1113 
1114 #endif // U_HIDE_INTERNAL_API
1115 
1116  using NumberFormat::parse;
1117 
1137  void parse(const UnicodeString& text, Formattable& result,
1138  ParsePosition& parsePosition) const U_OVERRIDE;
1139 
1159  CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const U_OVERRIDE;
1160 
1168  virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
1169 
1176  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1177 
1184  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1185 
1186 
1193  virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1194 
1201  virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
1202 
1209  virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
1210 
1211 
1220  UnicodeString& getPositivePrefix(UnicodeString& result) const;
1221 
1229  virtual void setPositivePrefix(const UnicodeString& newValue);
1230 
1239  UnicodeString& getNegativePrefix(UnicodeString& result) const;
1240 
1248  virtual void setNegativePrefix(const UnicodeString& newValue);
1249 
1258  UnicodeString& getPositiveSuffix(UnicodeString& result) const;
1259 
1267  virtual void setPositiveSuffix(const UnicodeString& newValue);
1268 
1277  UnicodeString& getNegativeSuffix(UnicodeString& result) const;
1278 
1286  virtual void setNegativeSuffix(const UnicodeString& newValue);
1287 
1288 #ifndef U_HIDE_INTERNAL_API
1289 
1293  UBool isSignAlwaysShown() const;
1294 #endif /* U_HIDE_INTERNAL_API */
1295 
1301  virtual void setSignAlwaysShown(UBool value);
1302 
1315  int32_t getMultiplier(void) const;
1316 
1330  virtual void setMultiplier(int32_t newValue);
1331 
1332 #ifndef U_HIDE_DRAFT_API
1333 
1345  int32_t getMultiplierScale(void) const;
1346 #endif /* U_HIDE_DRAFT_API */
1347 
1367  virtual void setMultiplierScale(int32_t newValue);
1368 
1378  virtual double getRoundingIncrement(void) const;
1379 
1391  virtual void setRoundingIncrement(double newValue);
1392 
1401  virtual ERoundingMode getRoundingMode(void) const U_OVERRIDE;
1402 
1411  virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1412 
1424  virtual int32_t getFormatWidth(void) const;
1425 
1440  virtual void setFormatWidth(int32_t width);
1441 
1454  virtual UnicodeString getPadCharacterString() const;
1455 
1470  virtual void setPadCharacter(const UnicodeString& padChar);
1471 
1487  virtual EPadPosition getPadPosition(void) const;
1488 
1505  virtual void setPadPosition(EPadPosition padPos);
1506 
1517  virtual UBool isScientificNotation(void) const;
1518 
1534  virtual void setScientificNotation(UBool useScientific);
1535 
1546  virtual int8_t getMinimumExponentDigits(void) const;
1547 
1560  virtual void setMinimumExponentDigits(int8_t minExpDig);
1561 
1574  virtual UBool isExponentSignAlwaysShown(void) const;
1575 
1589  virtual void setExponentSignAlwaysShown(UBool expSignAlways);
1590 
1602  int32_t getGroupingSize(void) const;
1603 
1615  virtual void setGroupingSize(int32_t newValue);
1616 
1635  int32_t getSecondaryGroupingSize(void) const;
1636 
1648  virtual void setSecondaryGroupingSize(int32_t newValue);
1649 
1650 #ifndef U_HIDE_INTERNAL_API
1651 
1673  int32_t getMinimumGroupingDigits() const;
1674 
1675 #endif /* U_HIDE_INTERNAL_API */
1676 
1677  /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual. */
1686  virtual void setMinimumGroupingDigits(int32_t newValue);
1687 
1688 
1697  UBool isDecimalSeparatorAlwaysShown(void) const;
1698 
1707  virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
1708 
1715  UBool isDecimalPatternMatchRequired(void) const;
1716 
1726  virtual void setDecimalPatternMatchRequired(UBool newValue);
1727 
1734  virtual UBool isParseNoExponent() const;
1735 
1744  virtual void setParseNoExponent(UBool value);
1745 
1752  virtual UBool isParseCaseSensitive() const;
1753 
1763  virtual void setParseCaseSensitive(UBool value);
1764 
1772  virtual UBool isFormatFailIfMoreThanMaxDigits() const;
1773 
1780  virtual void setFormatFailIfMoreThanMaxDigits(UBool value);
1781 
1782 
1793  virtual UnicodeString& toPattern(UnicodeString& result) const;
1794 
1805  virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
1806 
1836  virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
1837 
1846  virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1847 
1878  virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
1879  UErrorCode& status);
1880 
1890  virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
1891 
1892 
1902  void setMaximumIntegerDigits(int32_t newValue) U_OVERRIDE;
1903 
1913  void setMinimumIntegerDigits(int32_t newValue) U_OVERRIDE;
1914 
1924  void setMaximumFractionDigits(int32_t newValue) U_OVERRIDE;
1925 
1935  void setMinimumFractionDigits(int32_t newValue) U_OVERRIDE;
1936 
1944  int32_t getMinimumSignificantDigits() const;
1945 
1953  int32_t getMaximumSignificantDigits() const;
1954 
1966  void setMinimumSignificantDigits(int32_t min);
1967 
1979  void setMaximumSignificantDigits(int32_t max);
1980 
1987  UBool areSignificantDigitsUsed() const;
1988 
1996  void setSignificantDigitsUsed(UBool useSignificantDigits);
1997 
2010  void setCurrency(const char16_t* theCurrency, UErrorCode& ec) U_OVERRIDE;
2011 
2017  virtual void setCurrency(const char16_t* theCurrency);
2018 
2026  void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec);
2027 
2032  UCurrencyUsage getCurrencyUsage() const;
2033 
2034 #ifndef U_HIDE_INTERNAL_API
2035 
2041  void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2042  UErrorCode& status) const;
2043 
2050  void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2051  UErrorCode& status) const;
2052 
2053 #endif
2054 
2055 #ifndef U_HIDE_DRAFT_API
2056 
2079  const number::LocalizedNumberFormatter& toNumberFormatter() const;
2080 #endif /* U_HIDE_DRAFT_API */
2081 
2093  static UClassID U_EXPORT2 getStaticClassID(void);
2094 
2106  UClassID getDynamicClassID(void) const U_OVERRIDE;
2107 
2108  private:
2109 
2111  void touch(UErrorCode& status);
2112 
2114  void touchNoError();
2115 
2127  void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2128  UErrorCode& status);
2129 
2130  const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2131 
2132  const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2133 
2134  static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2135  int32_t offset, UErrorCode& status);
2136 
2137  static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2138  FieldPositionIterator* fpi, int32_t offset, UErrorCode& status);
2139 
2140  void setupFastFormat();
2141 
2142  bool fastFormatDouble(double input, UnicodeString& output) const;
2143 
2144  bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2145 
2146  void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2147 
2148  //=====================================================================================//
2149  // INSTANCE FIELDS //
2150  //=====================================================================================//
2151 
2152  // Only one instance field: keep all fields inside of an implementation class defined in number_mapper.h
2153  number::impl::DecimalFormatFields* fields;
2154 
2155  // Allow child class CompactDecimalFormat to access fProperties:
2156  friend class CompactDecimalFormat;
2157 
2158 };
2159 
2161 
2162 #endif /* #if !UCONFIG_NO_FORMATTING */
2163 
2164 #endif // _DECIMFMT
2165 //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
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&#39;s RTTI.
Definition: uobject.h:93
Limit of boolean attributes.
Definition: unum.h:1077
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
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:678
One below the first bitfield-boolean item.
Definition: unum.h:1048
#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:396
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:185
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:286
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:960
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:236