ICU 76.1  76.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 namespace impl {
57 class DecimalQuantity;
58 struct DecimalFormatFields;
59 class UFormattedNumberData;
60 }
61 }
62 
63 namespace numparse::impl {
64 class NumberParserImpl;
65 }
66 
667  public:
673  kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
674  };
675 
692  DecimalFormat(UErrorCode& status);
693 
711  DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
712 
734  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
735 
736 #ifndef U_HIDE_INTERNAL_API
737 
750  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
751  UNumberFormatStyle style, UErrorCode& status);
752 
753 #if UCONFIG_HAVE_PARSEALLINPUT
754 
758  void setParseAllInput(UNumberFormatAttributeValue value);
759 
760 #endif
761 
762 #endif /* U_HIDE_INTERNAL_API */
763 
764  private:
765 
770  DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
771 
772  public:
773 
784  virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
785 
795  virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
796 
797 
804  void setGroupingUsed(UBool newValue) override;
805 
813  void setParseIntegerOnly(UBool value) override;
814 
822  void setLenient(UBool enable) override;
823 
846  DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
847  UParseError& parseError, UErrorCode& status);
848 
869  DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
870 
877  DecimalFormat(const DecimalFormat& source);
878 
886 
891  ~DecimalFormat() override;
892 
900  DecimalFormat* clone() const override;
901 
910  bool operator==(const Format& other) const override;
911 
912 
913  using NumberFormat::format;
914 
926  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const override;
927 
928 #ifndef U_HIDE_INTERNAL_API
929 
941  UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
942  UErrorCode& status) const override;
943 #endif /* U_HIDE_INTERNAL_API */
944 
958  UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
959  UErrorCode& status) const override;
960 
972  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
973 
974 #ifndef U_HIDE_INTERNAL_API
975 
987  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
988  UErrorCode& status) const override;
989 #endif /* U_HIDE_INTERNAL_API */
990 
1004  UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1005  UErrorCode& status) const override;
1006 
1018  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
1019 
1020 #ifndef U_HIDE_INTERNAL_API
1021 
1033  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1034  UErrorCode& status) const override;
1035 #endif /* U_HIDE_INTERNAL_API */
1036 
1050  UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
1051  UErrorCode& status) const override;
1052 
1070  UErrorCode& status) const override;
1071 
1072 #ifndef U_HIDE_INTERNAL_API
1073 
1089  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1090  FieldPositionIterator* posIter, UErrorCode& status) const override;
1091 
1107  UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1108  FieldPosition& pos, UErrorCode& status) const override;
1109 
1110 #endif // U_HIDE_INTERNAL_API
1111 
1112  using NumberFormat::parse;
1113 
1133  void parse(const UnicodeString& text, Formattable& result,
1134  ParsePosition& parsePosition) const override;
1135 
1155  CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override;
1156 
1164  virtual const DecimalFormatSymbols* getDecimalFormatSymbols() const;
1165 
1172  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1173 
1180  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1181 
1182 
1189  virtual const CurrencyPluralInfo* getCurrencyPluralInfo() const;
1190 
1197  virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
1198 
1205  virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
1206 
1207 
1216  UnicodeString& getPositivePrefix(UnicodeString& result) const;
1217 
1225  virtual void setPositivePrefix(const UnicodeString& newValue);
1226 
1235  UnicodeString& getNegativePrefix(UnicodeString& result) const;
1236 
1244  virtual void setNegativePrefix(const UnicodeString& newValue);
1245 
1254  UnicodeString& getPositiveSuffix(UnicodeString& result) const;
1255 
1263  virtual void setPositiveSuffix(const UnicodeString& newValue);
1264 
1273  UnicodeString& getNegativeSuffix(UnicodeString& result) const;
1274 
1282  virtual void setNegativeSuffix(const UnicodeString& newValue);
1283 
1292  UBool isSignAlwaysShown() const;
1293 
1302  void setSignAlwaysShown(UBool value);
1303 
1316  int32_t getMultiplier() const;
1317 
1331  virtual void setMultiplier(int32_t newValue);
1332 
1345  int32_t getMultiplierScale() const;
1346 
1366  void setMultiplierScale(int32_t newValue);
1367 
1377  virtual double getRoundingIncrement() const;
1378 
1390  virtual void setRoundingIncrement(double newValue);
1391 
1400  virtual ERoundingMode getRoundingMode() const override;
1401 
1410  virtual void setRoundingMode(ERoundingMode roundingMode) override;
1411 
1423  virtual int32_t getFormatWidth() const;
1424 
1439  virtual void setFormatWidth(int32_t width);
1440 
1453  virtual UnicodeString getPadCharacterString() const;
1454 
1469  virtual void setPadCharacter(const UnicodeString& padChar);
1470 
1486  virtual EPadPosition getPadPosition() const;
1487 
1504  virtual void setPadPosition(EPadPosition padPos);
1505 
1516  virtual UBool isScientificNotation() const;
1517 
1533  virtual void setScientificNotation(UBool useScientific);
1534 
1545  virtual int8_t getMinimumExponentDigits() const;
1546 
1559  virtual void setMinimumExponentDigits(int8_t minExpDig);
1560 
1573  virtual UBool isExponentSignAlwaysShown() const;
1574 
1588  virtual void setExponentSignAlwaysShown(UBool expSignAlways);
1589 
1601  int32_t getGroupingSize() const;
1602 
1614  virtual void setGroupingSize(int32_t newValue);
1615 
1634  int32_t getSecondaryGroupingSize() const;
1635 
1647  virtual void setSecondaryGroupingSize(int32_t newValue);
1648 
1672  int32_t getMinimumGroupingDigits() const;
1673 
1691  void setMinimumGroupingDigits(int32_t newValue);
1692 
1701  UBool isDecimalSeparatorAlwaysShown() const;
1702 
1711  virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
1712 
1719  UBool isDecimalPatternMatchRequired() const;
1720 
1730  virtual void setDecimalPatternMatchRequired(UBool newValue);
1731 
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 
1802  virtual UnicodeString& toPattern(UnicodeString& result) const;
1803 
1814  virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
1815 
1845  virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
1846 
1855  virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1856 
1887  virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
1888  UErrorCode& status);
1889 
1899  virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
1900 
1901 
1911  void setMaximumIntegerDigits(int32_t newValue) override;
1912 
1922  void setMinimumIntegerDigits(int32_t newValue) override;
1923 
1933  void setMaximumFractionDigits(int32_t newValue) override;
1934 
1944  void setMinimumFractionDigits(int32_t newValue) override;
1945 
1953  int32_t getMinimumSignificantDigits() const;
1954 
1962  int32_t getMaximumSignificantDigits() const;
1963 
1975  void setMinimumSignificantDigits(int32_t min);
1976 
1988  void setMaximumSignificantDigits(int32_t max);
1989 
1996  UBool areSignificantDigitsUsed() const;
1997 
2005  void setSignificantDigitsUsed(UBool useSignificantDigits);
2006 
2019  void setCurrency(const char16_t* theCurrency, UErrorCode& ec) override;
2020 
2021 #ifndef U_FORCE_HIDE_DEPRECATED_API
2022 
2027  virtual void setCurrency(const char16_t* theCurrency);
2028 #endif // U_FORCE_HIDE_DEPRECATED_API
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 
2112  const number::LocalizedNumberFormatter* toNumberFormatter(UErrorCode& status) const;
2113 
2125  static UClassID U_EXPORT2 getStaticClassID();
2126 
2138  UClassID getDynamicClassID() const override;
2139 
2140  private:
2141 
2143  void touch(UErrorCode& status);
2144 
2146  void touchNoError();
2147 
2159  void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2160  UErrorCode& status);
2161 
2162  const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2163 
2164  const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2165 
2166  static void fieldPositionHelper(
2167  const number::impl::UFormattedNumberData& formatted,
2168  FieldPosition& fieldPosition,
2169  int32_t offset,
2170  UErrorCode& status);
2171 
2172  static void fieldPositionIteratorHelper(
2173  const number::impl::UFormattedNumberData& formatted,
2174  FieldPositionIterator* fpi,
2175  int32_t offset,
2176  UErrorCode& status);
2177 
2178  void setupFastFormat();
2179 
2180  bool fastFormatDouble(double input, UnicodeString& output) const;
2181 
2182  bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2183 
2184  void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2185 
2186  //=====================================================================================//
2187  // INSTANCE FIELDS //
2188  //=====================================================================================//
2189 
2190 
2191  // One instance field for the implementation, keep all fields inside of an implementation
2192  // class defined in number_mapper.h
2193  number::impl::DecimalFormatFields* fields = nullptr;
2194 
2195  // Allow child class CompactDecimalFormat to access fProperties:
2196  friend class CompactDecimalFormat;
2197 
2198  // Allow MeasureFormat to use fieldPositionHelper:
2199  friend class MeasureFormat;
2200 
2201 };
2202 
2203 U_NAMESPACE_END
2204 
2205 #endif /* #if !UCONFIG_NO_FORMATTING */
2206 
2207 #endif /* U_SHOW_CPLUSPLUS_API */
2208 
2209 #endif // _DECIMFMT
2210 //eof
Base class for all formats.
Definition: format.h:98
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case...
Definition: decimfmt.h:666
virtual NumberFormat * clone() const override=0
Clones this object polymorphically.
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
C++ API: FieldPosition Iterator.
virtual void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
static UClassID getStaticClassID()
Return the class ID for this class.
virtual void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
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:316
C++ API: StringPiece: Read-only byte string wrapper class.
C++ API: Currency Amount Object.
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 UClassID getDynamicClassID() const override=0
Returns a unique class ID POLYMORPHICALLY.
UNumberFormatStyle
The possible number format styles.
Definition: unum.h:150
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
virtual void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
virtual ERoundingMode getRoundingMode() const
Get the rounding mode.
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
virtual void setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
EPadPosition
Pad position.
Definition: decimfmt.h:672
virtual void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
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:52
virtual void setCurrency(const char16_t *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
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:48
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:295
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
C++: internal template EnumSet<>
C++ API: Compatibility APIs for number formatting.
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition: unum.h:970
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:61
virtual void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
A currency together with a numeric amount, such as 200 USD.
Definition: curramt.h:39
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247