ICU 65.1  65.1
numberformatter.h
Go to the documentation of this file.
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #ifndef __NUMBERFORMATTER_H__
5 #define __NUMBERFORMATTER_H__
6 
7 #include "unicode/utypes.h"
8 
9 #if U_SHOW_CPLUSPLUS_API
10 
11 #if !UCONFIG_NO_FORMATTING
12 
13 #include "unicode/appendable.h"
14 #include "unicode/bytestream.h"
15 #include "unicode/currunit.h"
16 #include "unicode/dcfmtsym.h"
17 #include "unicode/fieldpos.h"
18 #include "unicode/formattedvalue.h"
19 #include "unicode/fpositer.h"
20 #include "unicode/measunit.h"
21 #include "unicode/nounit.h"
22 #include "unicode/parseerr.h"
23 #include "unicode/plurrule.h"
24 #include "unicode/ucurr.h"
25 #include "unicode/unum.h"
27 #include "unicode/uobject.h"
28 
85 U_NAMESPACE_BEGIN
86 
87 // Forward declarations:
88 class IFixedDecimal;
89 class FieldPositionIteratorHandler;
90 class FormattedStringBuilder;
91 
92 namespace numparse {
93 namespace impl {
94 
95 // Forward declarations:
96 class NumberParserImpl;
97 class MultiplierParseHandler;
98 
99 }
100 }
101 
102 namespace number { // icu::number
103 
104 // Forward declarations:
105 class UnlocalizedNumberFormatter;
106 class LocalizedNumberFormatter;
107 class FormattedNumber;
108 class Notation;
109 class ScientificNotation;
110 class Precision;
111 class FractionPrecision;
112 class CurrencyPrecision;
113 class IncrementPrecision;
114 class IntegerWidth;
115 
116 namespace impl {
117 
118 // can't be #ifndef U_HIDE_INTERNAL_API; referenced throughout this file in public classes
124 typedef int16_t digits_t;
125 
126 // can't be #ifndef U_HIDE_INTERNAL_API; needed for struct initialization
133 static constexpr int32_t kInternalDefaultThreshold = 3;
134 
135 // Forward declarations:
136 class Padder;
137 struct MacroProps;
138 struct MicroProps;
139 class DecimalQuantity;
140 class UFormattedNumberData;
141 class NumberFormatterImpl;
142 struct ParsedPatternInfo;
143 class ScientificModifier;
144 class MultiplierProducer;
145 class RoundingImpl;
146 class ScientificHandler;
147 class Modifier;
148 class AffixPatternProvider;
149 class NumberPropertyMapper;
150 struct DecimalFormatProperties;
151 class MultiplierFormatHandler;
152 class CurrencySymbols;
153 class GeneratorHelpers;
154 class DecNum;
155 class NumberRangeFormatterImpl;
156 struct RangeMacroProps;
157 struct UFormattedNumberImpl;
158 
166 
167 } // namespace impl
168 
175 
182 
188 class U_I18N_API Notation : public UMemory {
189  public:
214  static ScientificNotation scientific();
215 
238  static ScientificNotation engineering();
239 
281  static CompactNotation compactShort();
282 
305  static CompactNotation compactLong();
306 
331  static SimpleNotation simple();
332 
333  private:
334  enum NotationType {
335  NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
336  } fType;
337 
338  union NotationUnion {
339  // For NTN_SCIENTIFIC
350  } scientific;
351 
352  // For NTN_COMPACT
353  UNumberCompactStyle compactStyle;
354 
355  // For NTN_ERROR
356  UErrorCode errorCode;
357  } fUnion;
358 
360 
361  Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
362 
363  Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
364  fUnion.errorCode = errorCode;
365  }
366 
367  Notation() : fType(NTN_SIMPLE), fUnion() {}
368 
369  UBool copyErrorTo(UErrorCode &status) const {
370  if (fType == NTN_ERROR) {
371  status = fUnion.errorCode;
372  return TRUE;
373  }
374  return FALSE;
375  }
376 
377  // To allow MacroProps to initialize empty instances:
378  friend struct impl::MacroProps;
379  friend class ScientificNotation;
380 
381  // To allow implementation to access internal types:
382  friend class impl::NumberFormatterImpl;
383  friend class impl::ScientificModifier;
384  friend class impl::ScientificHandler;
385 
386  // To allow access to the skeleton generation code:
387  friend class impl::GeneratorHelpers;
388 };
389 
399  public:
413  ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const;
414 
428  ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const;
429 
430  private:
431  // Inherit constructor
432  using Notation::Notation;
433 
434  // Raw constructor for NumberPropertyMapper
435  ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
436  UNumberSignDisplay fExponentSignDisplay);
437 
438  friend class Notation;
439 
440  // So that NumberPropertyMapper can create instances
441  friend class impl::NumberPropertyMapper;
442 };
443 
450 
459 class U_I18N_API Precision : public UMemory {
460 
461  public:
479  static Precision unlimited();
480 
487  static FractionPrecision integer();
488 
516  static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces);
517 
531  static FractionPrecision minFraction(int32_t minFractionPlaces);
532 
543  static FractionPrecision maxFraction(int32_t maxFractionPlaces);
544 
558  static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces);
559 
573  static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits);
574 
587  static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits);
588 
597  static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits);
598 
610  static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits,
611  int32_t maxSignificantDigits);
612 
632  static IncrementPrecision increment(double roundingIncrement);
633 
651  static CurrencyPrecision currency(UCurrencyUsage currencyUsage);
652 
653  private:
654  enum PrecisionType {
655  RND_BOGUS,
656  RND_NONE,
657  RND_FRACTION,
658  RND_SIGNIFICANT,
659  RND_FRACTION_SIGNIFICANT,
660 
661  // Used for strange increments like 3.14.
662  RND_INCREMENT,
663 
664  // Used for increments with 1 as the only digit. This is different than fraction
665  // rounding because it supports having additional trailing zeros. For example, this
666  // class is used to round with the increment 0.010.
667  RND_INCREMENT_ONE,
668 
669  // Used for increments with 5 as the only digit (nickel rounding).
670  RND_INCREMENT_FIVE,
671 
672  RND_CURRENCY,
673  RND_ERROR
674  } fType;
675 
676  union PrecisionUnion {
679  // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
688  } fracSig;
691  // For RND_INCREMENT, RND_INCREMENT_ONE, and RND_INCREMENT_FIVE
693  double fIncrement;
698  } increment;
699  UCurrencyUsage currencyUsage; // For RND_CURRENCY
700  UErrorCode errorCode; // For RND_ERROR
701  } fUnion;
702 
705 
707  UNumberFormatRoundingMode fRoundingMode;
708 
709  Precision(const PrecisionType& type, const PrecisionUnion& union_,
710  UNumberFormatRoundingMode roundingMode)
711  : fType(type), fUnion(union_), fRoundingMode(roundingMode) {}
712 
713  Precision(UErrorCode errorCode) : fType(RND_ERROR) {
714  fUnion.errorCode = errorCode;
715  }
716 
717  Precision() : fType(RND_BOGUS) {}
718 
719  bool isBogus() const {
720  return fType == RND_BOGUS;
721  }
722 
723  UBool copyErrorTo(UErrorCode &status) const {
724  if (fType == RND_ERROR) {
725  status = fUnion.errorCode;
726  return TRUE;
727  }
728  return FALSE;
729  }
730 
731  // On the parent type so that this method can be called internally on Precision instances.
732  Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
733 
734  static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
735 
736  static Precision constructSignificant(int32_t minSig, int32_t maxSig);
737 
738  static Precision
739  constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig);
740 
741  static IncrementPrecision constructIncrement(double increment, int32_t minFrac);
742 
743  static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
744 
745  static Precision constructPassThrough();
746 
747  // To allow MacroProps/MicroProps to initialize bogus instances:
748  friend struct impl::MacroProps;
749  friend struct impl::MicroProps;
750 
751  // To allow NumberFormatterImpl to access isBogus() and other internal methods:
752  friend class impl::NumberFormatterImpl;
753 
754  // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
755  friend class impl::NumberPropertyMapper;
756 
757  // To allow access to the main implementation class:
758  friend class impl::RoundingImpl;
759 
760  // To allow child classes to call private methods:
761  friend class FractionPrecision;
762  friend class CurrencyPrecision;
763  friend class IncrementPrecision;
764 
765  // To allow access to the skeleton generation code:
766  friend class impl::GeneratorHelpers;
767 };
768 
779  public:
796  Precision withMinDigits(int32_t minSignificantDigits) const;
797 
815  Precision withMaxDigits(int32_t maxSignificantDigits) const;
816 
817  private:
818  // Inherit constructor
819  using Precision::Precision;
820 
821  // To allow parent class to call this class's constructor:
822  friend class Precision;
823 };
824 
835  public:
853  Precision withCurrency(const CurrencyUnit &currency) const;
854 
855  private:
856  // Inherit constructor
857  using Precision::Precision;
858 
859  // To allow parent class to call this class's constructor:
860  friend class Precision;
861 };
862 
873  public:
889  Precision withMinFraction(int32_t minFrac) const;
890 
891  private:
892  // Inherit constructor
893  using Precision::Precision;
894 
895  // To allow parent class to call this class's constructor:
896  friend class Precision;
897 };
898 
909  public:
921  static IntegerWidth zeroFillTo(int32_t minInt);
922 
934  IntegerWidth truncateAt(int32_t maxInt);
935 
936  private:
937  union {
938  struct {
939  impl::digits_t fMinInt;
940  impl::digits_t fMaxInt;
941  bool fFormatFailIfMoreThanMaxDigits;
942  } minMaxInt;
943  UErrorCode errorCode;
944  } fUnion;
945  bool fHasError = false;
946 
947  IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
948 
949  IntegerWidth(UErrorCode errorCode) { // NOLINT
950  fUnion.errorCode = errorCode;
951  fHasError = true;
952  }
953 
954  IntegerWidth() { // NOLINT
955  fUnion.minMaxInt.fMinInt = -1;
956  }
957 
959  static IntegerWidth standard() {
960  return IntegerWidth::zeroFillTo(1);
961  }
962 
963  bool isBogus() const {
964  return !fHasError && fUnion.minMaxInt.fMinInt == -1;
965  }
966 
967  UBool copyErrorTo(UErrorCode &status) const {
968  if (fHasError) {
969  status = fUnion.errorCode;
970  return TRUE;
971  }
972  return FALSE;
973  }
974 
975  void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
976 
977  bool operator==(const IntegerWidth& other) const;
978 
979  // To allow MacroProps/MicroProps to initialize empty instances:
980  friend struct impl::MacroProps;
981  friend struct impl::MicroProps;
982 
983  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
984  friend class impl::NumberFormatterImpl;
985 
986  // So that NumberPropertyMapper can create instances
987  friend class impl::NumberPropertyMapper;
988 
989  // To allow access to the skeleton generation code:
990  friend class impl::GeneratorHelpers;
991 };
992 
1001 class U_I18N_API Scale : public UMemory {
1002  public:
1009  static Scale none();
1010 
1021  static Scale powerOfTen(int32_t power);
1022 
1035  static Scale byDecimal(StringPiece multiplicand);
1036 
1045  static Scale byDouble(double multiplicand);
1046 
1053  static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power);
1054 
1055  // We need a custom destructor for the DecNum, which means we need to declare
1056  // the copy/move constructor/assignment quartet.
1057 
1059  Scale(const Scale& other);
1060 
1062  Scale& operator=(const Scale& other);
1063 
1065  Scale(Scale&& src) U_NOEXCEPT;
1066 
1068  Scale& operator=(Scale&& src) U_NOEXCEPT;
1069 
1071  ~Scale();
1072 
1073 #ifndef U_HIDE_INTERNAL_API
1074 
1075  Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1076 #endif /* U_HIDE_INTERNAL_API */
1077 
1078  private:
1079  int32_t fMagnitude;
1080  impl::DecNum* fArbitrary;
1081  UErrorCode fError;
1082 
1083  Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1084 
1085  Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1086 
1087  bool isValid() const {
1088  return fMagnitude != 0 || fArbitrary != nullptr;
1089  }
1090 
1091  UBool copyErrorTo(UErrorCode &status) const {
1092  if (fError != U_ZERO_ERROR) {
1093  status = fError;
1094  return TRUE;
1095  }
1096  return FALSE;
1097  }
1098 
1099  void applyTo(impl::DecimalQuantity& quantity) const;
1100 
1101  void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1102 
1103  // To allow MacroProps/MicroProps to initialize empty instances:
1104  friend struct impl::MacroProps;
1105  friend struct impl::MicroProps;
1106 
1107  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1108  friend class impl::NumberFormatterImpl;
1109 
1110  // To allow the helper class MultiplierFormatHandler access to private fields:
1111  friend class impl::MultiplierFormatHandler;
1112 
1113  // To allow access to the skeleton generation code:
1114  friend class impl::GeneratorHelpers;
1115 
1116  // To allow access to parsing code:
1117  friend class ::icu::numparse::impl::NumberParserImpl;
1118  friend class ::icu::numparse::impl::MultiplierParseHandler;
1119 };
1120 
1121 namespace impl {
1122 
1123 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1126  public:
1128  SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1129 
1131  SymbolsWrapper(const SymbolsWrapper &other);
1132 
1134  SymbolsWrapper &operator=(const SymbolsWrapper &other);
1135 
1137  SymbolsWrapper(SymbolsWrapper&& src) U_NOEXCEPT;
1138 
1140  SymbolsWrapper &operator=(SymbolsWrapper&& src) U_NOEXCEPT;
1141 
1143  ~SymbolsWrapper();
1144 
1145 #ifndef U_HIDE_INTERNAL_API
1146 
1151  void setTo(const DecimalFormatSymbols &dfs);
1152 
1157  void setTo(const NumberingSystem *ns);
1158 
1163  bool isDecimalFormatSymbols() const;
1164 
1169  bool isNumberingSystem() const;
1170 
1175  const DecimalFormatSymbols *getDecimalFormatSymbols() const;
1176 
1181  const NumberingSystem *getNumberingSystem() const;
1182 
1183 #endif // U_HIDE_INTERNAL_API
1184 
1186  UBool copyErrorTo(UErrorCode &status) const {
1187  if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1188  status = U_MEMORY_ALLOCATION_ERROR;
1189  return TRUE;
1190  } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1191  status = U_MEMORY_ALLOCATION_ERROR;
1192  return TRUE;
1193  }
1194  return FALSE;
1195  }
1196 
1197  private:
1198  enum SymbolsPointerType {
1199  SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1200  } fType;
1201 
1202  union {
1203  const DecimalFormatSymbols *dfs;
1204  const NumberingSystem *ns;
1205  } fPtr;
1206 
1207  void doCopyFrom(const SymbolsWrapper &other);
1208 
1209  void doMoveFrom(SymbolsWrapper&& src);
1210 
1211  void doCleanup();
1212 };
1213 
1214 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1216 class U_I18N_API Grouper : public UMemory {
1217  public:
1218 #ifndef U_HIDE_INTERNAL_API
1219 
1220  static Grouper forStrategy(UNumberGroupingStrategy grouping);
1221 
1226  static Grouper forProperties(const DecimalFormatProperties& properties);
1227 
1228  // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1229 
1231  Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
1232  : fGrouping1(grouping1),
1233  fGrouping2(grouping2),
1234  fMinGrouping(minGrouping),
1235  fStrategy(strategy) {}
1236 #endif // U_HIDE_INTERNAL_API
1237 
1239  int16_t getPrimary() const;
1240 
1242  int16_t getSecondary() const;
1243 
1244  private:
1253  int16_t fGrouping1;
1254  int16_t fGrouping2;
1255 
1263  int16_t fMinGrouping;
1264 
1269  UNumberGroupingStrategy fStrategy;
1270 
1271  Grouper() : fGrouping1(-3) {}
1272 
1273  bool isBogus() const {
1274  return fGrouping1 == -3;
1275  }
1276 
1278  void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1279 
1280  bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1281 
1282  // To allow MacroProps/MicroProps to initialize empty instances:
1283  friend struct MacroProps;
1284  friend struct MicroProps;
1285 
1286  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1287  friend class NumberFormatterImpl;
1288 
1289  // To allow NumberParserImpl to perform setLocaleData():
1290  friend class ::icu::numparse::impl::NumberParserImpl;
1291 
1292  // To allow access to the skeleton generation code:
1293  friend class impl::GeneratorHelpers;
1294 };
1295 
1296 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1298 class U_I18N_API Padder : public UMemory {
1299  public:
1300 #ifndef U_HIDE_INTERNAL_API
1301 
1302  static Padder none();
1303 
1305  static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position);
1306 #endif // U_HIDE_INTERNAL_API
1307 
1309  static Padder forProperties(const DecimalFormatProperties& properties);
1310 
1311  private:
1312  UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1313  union {
1314  struct {
1315  int32_t fCp;
1316  UNumberFormatPadPosition fPosition;
1317  } padding;
1318  UErrorCode errorCode;
1319  } fUnion;
1320 
1321  Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1322 
1323  Padder(int32_t width);
1324 
1325  Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1326  fUnion.errorCode = errorCode;
1327  }
1328 
1329  Padder() : fWidth(-2) {} // NOLINT
1330 
1331  bool isBogus() const {
1332  return fWidth == -2;
1333  }
1334 
1335  UBool copyErrorTo(UErrorCode &status) const {
1336  if (fWidth == -3) {
1337  status = fUnion.errorCode;
1338  return TRUE;
1339  }
1340  return FALSE;
1341  }
1342 
1343  bool isValid() const {
1344  return fWidth > 0;
1345  }
1346 
1347  int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1348  FormattedStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1349  UErrorCode &status) const;
1350 
1351  // To allow MacroProps/MicroProps to initialize empty instances:
1352  friend struct MacroProps;
1353  friend struct MicroProps;
1354 
1355  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1356  friend class impl::NumberFormatterImpl;
1357 
1358  // To allow access to the skeleton generation code:
1359  friend class impl::GeneratorHelpers;
1360 };
1361 
1362 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1364 struct U_I18N_API MacroProps : public UMemory {
1367 
1369  MeasureUnit unit; // = NoUnit::base();
1370 
1372  MeasureUnit perUnit; // = NoUnit::base();
1373 
1375  Precision precision; // = Precision(); (bogus)
1376 
1379 
1381  Grouper grouper; // = Grouper(); (bogus)
1382 
1384  Padder padder; // = Padder(); (bogus)
1385 
1387  IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1388 
1391 
1392  // UNUM_XYZ_COUNT denotes null (bogus) values.
1393 
1396 
1399 
1402 
1404  Scale scale; // = Scale(); (benign value)
1405 
1407  const AffixPatternProvider* affixProvider = nullptr; // no ownership
1408 
1410  const PluralRules* rules = nullptr; // no ownership
1411 
1413  const CurrencySymbols* currencySymbols = nullptr; // no ownership
1414 
1416  int32_t threshold = kInternalDefaultThreshold;
1417 
1420 
1421  // NOTE: Uses default copy and move constructors.
1422 
1427  bool copyErrorTo(UErrorCode &status) const {
1428  return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1429  padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1430  symbols.copyErrorTo(status) || scale.copyErrorTo(status);
1431  }
1432 };
1433 
1434 } // namespace impl
1435 
1441 template<typename Derived>
1443  public:
1472  Derived notation(const Notation &notation) const &;
1473 
1483  Derived notation(const Notation &notation) &&;
1484 
1528  Derived unit(const icu::MeasureUnit &unit) const &;
1529 
1539  Derived unit(const icu::MeasureUnit &unit) &&;
1540 
1554  Derived adoptUnit(icu::MeasureUnit *unit) const &;
1555 
1565  Derived adoptUnit(icu::MeasureUnit *unit) &&;
1566 
1589  Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1590 
1600  Derived perUnit(const icu::MeasureUnit &perUnit) &&;
1601 
1615  Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &;
1616 
1626  Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&;
1627 
1658  Derived precision(const Precision& precision) const &;
1659 
1669  Derived precision(const Precision& precision) &&;
1670 
1689  Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &;
1690 
1699  Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&;
1700 
1728  Derived grouping(UNumberGroupingStrategy strategy) const &;
1729 
1739  Derived grouping(UNumberGroupingStrategy strategy) &&;
1740 
1765  Derived integerWidth(const IntegerWidth &style) const &;
1766 
1776  Derived integerWidth(const IntegerWidth &style) &&;
1777 
1818  Derived symbols(const DecimalFormatSymbols &symbols) const &;
1819 
1829  Derived symbols(const DecimalFormatSymbols &symbols) &&;
1830 
1864  Derived adoptSymbols(NumberingSystem *symbols) const &;
1865 
1875  Derived adoptSymbols(NumberingSystem *symbols) &&;
1876 
1902  Derived unitWidth(UNumberUnitWidth width) const &;
1903 
1913  Derived unitWidth(UNumberUnitWidth width) &&;
1914 
1940  Derived sign(UNumberSignDisplay style) const &;
1941 
1951  Derived sign(UNumberSignDisplay style) &&;
1952 
1978  Derived decimal(UNumberDecimalSeparatorDisplay style) const &;
1979 
1989  Derived decimal(UNumberDecimalSeparatorDisplay style) &&;
1990 
2015  Derived scale(const Scale &scale) const &;
2016 
2026  Derived scale(const Scale &scale) &&;
2027 
2028 #ifndef U_HIDE_INTERNAL_API
2029 
2035  Derived padding(const impl::Padder &padder) const &;
2036 
2038  Derived padding(const impl::Padder &padder) &&;
2039 
2046  Derived threshold(int32_t threshold) const &;
2047 
2049  Derived threshold(int32_t threshold) &&;
2050 
2056  Derived macros(const impl::MacroProps& macros) const &;
2057 
2059  Derived macros(const impl::MacroProps& macros) &&;
2060 
2062  Derived macros(impl::MacroProps&& macros) const &;
2063 
2065  Derived macros(impl::MacroProps&& macros) &&;
2066 
2067 #endif /* U_HIDE_INTERNAL_API */
2068 
2083  UnicodeString toSkeleton(UErrorCode& status) const;
2084 
2085 #ifndef U_HIDE_DRAFT_API
2086 
2097  LocalPointer<Derived> clone() const &;
2098 
2106  LocalPointer<Derived> clone() &&;
2107 #endif /* U_HIDE_DRAFT_API */
2108 
2115  UBool copyErrorTo(UErrorCode &outErrorCode) const {
2116  if (U_FAILURE(outErrorCode)) {
2117  // Do not overwrite the older error code
2118  return TRUE;
2119  }
2120  fMacros.copyErrorTo(outErrorCode);
2121  return U_FAILURE(outErrorCode);
2122  }
2123 
2124  // NOTE: Uses default copy and move constructors.
2125 
2126  private:
2127  impl::MacroProps fMacros;
2128 
2129  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2130  NumberFormatterSettings() = default;
2131 
2132  friend class LocalizedNumberFormatter;
2133  friend class UnlocalizedNumberFormatter;
2134 
2135  // Give NumberRangeFormatter access to the MacroProps
2136  friend void impl::touchRangeLocales(impl::RangeMacroProps& macros);
2137  friend class impl::NumberRangeFormatterImpl;
2138 };
2139 
2149  : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory {
2150 
2151  public:
2161  LocalizedNumberFormatter locale(const icu::Locale &locale) const &;
2162 
2172  LocalizedNumberFormatter locale(const icu::Locale &locale) &&;
2173 
2179  UnlocalizedNumberFormatter() = default;
2180 
2186 
2193 
2198  UnlocalizedNumberFormatter& operator=(const UnlocalizedNumberFormatter& other);
2199 
2206 
2207  private:
2209 
2210  explicit UnlocalizedNumberFormatter(
2212 
2213  // To give the fluent setters access to this class's constructor:
2215 
2216  // To give NumberFormatter::with() access to this class's constructor:
2217  friend class NumberFormatter;
2218 };
2219 
2229  : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory {
2230  public:
2242  FormattedNumber formatInt(int64_t value, UErrorCode &status) const;
2243 
2255  FormattedNumber formatDouble(double value, UErrorCode &status) const;
2256 
2271  FormattedNumber formatDecimal(StringPiece value, UErrorCode& status) const;
2272 
2273 #ifndef U_HIDE_INTERNAL_API
2274 
2278  FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2279 
2283  void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const;
2284 
2289  const impl::NumberFormatterImpl* getCompiled() const;
2290 
2295  int32_t getCallCount() const;
2296 
2297 #endif /* U_HIDE_INTERNAL_API */
2298 
2312  Format* toFormat(UErrorCode& status) const;
2313 
2319  LocalizedNumberFormatter() = default;
2320 
2326 
2333 
2338  LocalizedNumberFormatter& operator=(const LocalizedNumberFormatter& other);
2339 
2346 
2347 #ifndef U_HIDE_INTERNAL_API
2348 
2361  void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2362 
2363 #endif /* U_HIDE_INTERNAL_API */
2364 
2370 
2371  private:
2372  // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2373  // header, and LocalPointer needs the full class definition in order to delete the instance.
2374  const impl::NumberFormatterImpl* fCompiled {nullptr};
2375  char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2376 
2378 
2380 
2381  LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2382 
2383  LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2384 
2385  void clear();
2386 
2387  void lnfMoveHelper(LocalizedNumberFormatter&& src);
2388 
2392  bool computeCompiled(UErrorCode& status) const;
2393 
2394  // To give the fluent setters access to this class's constructor:
2397 
2398  // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2399  friend class UnlocalizedNumberFormatter;
2400 };
2401 
2411  public:
2412 
2413  // Default constructor cannot have #ifndef U_HIDE_DRAFT_API
2414 #ifndef U_FORCE_HIDE_DRAFT_API
2415 
2420  : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
2421 #endif // U_FORCE_HIDE_DRAFT_API
2422 
2428 
2433  virtual ~FormattedNumber() U_OVERRIDE;
2434 
2436  FormattedNumber(const FormattedNumber&) = delete;
2437 
2439  FormattedNumber& operator=(const FormattedNumber&) = delete;
2440 
2445  FormattedNumber& operator=(FormattedNumber&& src) U_NOEXCEPT;
2446 
2447  // Copybrief: this method is older than the parent method
2455  UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
2456 
2457  // Copydoc: this method is new in ICU 64
2459  UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
2460 
2461  // Copybrief: this method is older than the parent method
2469  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
2470 
2471  // Copydoc: this method is new in ICU 64
2473  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
2474 
2475 #ifndef U_HIDE_DRAFT_API
2476 
2509  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
2510 
2526  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
2527 #endif /* U_HIDE_DRAFT_API */
2528 
2529 #ifndef U_HIDE_DRAFT_API
2530 
2548  template<typename StringClass>
2549  inline StringClass toDecimalNumber(UErrorCode& status) const;
2550 #endif // U_HIDE_DRAFT_API
2551 
2552 #ifndef U_HIDE_INTERNAL_API
2553 
2558  void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
2559 
2564  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
2565 
2566 #endif /* U_HIDE_INTERNAL_API */
2567 
2568  private:
2569  // Can't use LocalPointer because UFormattedNumberData is forward-declared
2570  const impl::UFormattedNumberData *fData;
2571 
2572  // Error code for the terminal methods
2573  UErrorCode fErrorCode;
2574 
2579  explicit FormattedNumber(impl::UFormattedNumberData *results)
2580  : fData(results), fErrorCode(U_ZERO_ERROR) {}
2581 
2582  explicit FormattedNumber(UErrorCode errorCode)
2583  : fData(nullptr), fErrorCode(errorCode) {}
2584 
2585  // TODO(ICU-20775): Propose this as API.
2586  void toDecimalNumber(ByteSink& sink, UErrorCode& status) const;
2587 
2588  // To give LocalizedNumberFormatter format methods access to this class's constructor:
2589  friend class LocalizedNumberFormatter;
2590 
2591  // To give C API access to internals
2592  friend struct impl::UFormattedNumberImpl;
2593 };
2594 
2595 #ifndef U_HIDE_DRAFT_API
2596 // Note: This is draft ICU 65
2597 template<typename StringClass>
2598 StringClass FormattedNumber::toDecimalNumber(UErrorCode& status) const {
2599  StringClass result;
2600  StringByteSink<StringClass> sink(&result);
2601  toDecimalNumber(sink, status);
2602  return result;
2603 };
2604 #endif // U_HIDE_DRAFT_API
2605 
2612  public:
2620  static UnlocalizedNumberFormatter with();
2621 
2631  static LocalizedNumberFormatter withLocale(const Locale &locale);
2632 
2647  static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, UErrorCode& status);
2648 
2649 #ifndef U_HIDE_DRAFT_API
2650 
2667  static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton,
2668  UParseError& perror, UErrorCode& status);
2669 #endif
2670 
2674  NumberFormatter() = delete;
2675 };
2676 
2677 } // namespace number
2678 U_NAMESPACE_END
2679 
2680 #endif /* #if !UCONFIG_NO_FORMATTING */
2681 
2682 #endif /* U_SHOW_CPLUSPLUS_API */
2683 
2684 #endif // __NUMBERFORMATTER_H__
2685 
icu::number::Scale
A class that defines a quantity by which a number should be multiplied when formatting.
Definition: numberformatter.h:1001
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
UNUM_DECIMAL_SEPARATOR_COUNT
@ UNUM_DECIMAL_SEPARATOR_COUNT
One more than the highest UNumberDecimalSeparatorDisplay value.
Definition: unumberformatter.h:395
icu::number::Notation::NotationUnion::ScientificSettings::fEngineeringInterval
int8_t fEngineeringInterval
Definition: numberformatter.h:343
FALSE
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:269
icu::MeasureUnit
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:40
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMaxSig
impl::digits_t fMaxSig
Definition: numberformatter.h:687
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
fpositer.h
C++ API: FieldPosition Iterator.
parseerr.h
C API: Parse Error Information.
icu::number::impl::MacroProps::perUnit
MeasureUnit perUnit
Definition: numberformatter.h:1372
unumberformatter.h
C-compatible API for localized number formatting; not recommended for C++.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::number::impl::touchRangeLocales
void touchRangeLocales(impl::RangeMacroProps &macros)
Used for NumberRangeFormatter and implemented in numrange_fluent.cpp.
icu::CurrencyUnit
A unit of currency, such as USD (U.S.
Definition: currunit.h:39
icu::number::IntegerWidth
A class that defines the strategy for padding and truncating integers before the decimal separator.
Definition: numberformatter.h:908
icu::number::impl::MacroProps::scale
Scale scale
Definition: numberformatter.h:1404
U_I18N_API
#define U_I18N_API
Definition: utypes.h:301
UNUM_UNIT_WIDTH_COUNT
@ UNUM_UNIT_WIDTH_COUNT
One more than the highest UNumberUnitWidth value.
Definition: unumberformatter.h:164
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
UNumberSignDisplay
UNumberSignDisplay
An enum declaring how to denote positive and negative numbers.
Definition: unumberformatter.h:287
U_NOEXCEPT
#define U_NOEXCEPT
Definition: platform.h:529
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:254
icu::number::IncrementPrecision
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
Definition: numberformatter.h:872
UNUM_SIGN_COUNT
@ UNUM_SIGN_COUNT
One more than the highest UNumberSignDisplay value.
Definition: unumberformatter.h:360
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
U_OVERRIDE
#define U_OVERRIDE
Definition: umachine.h:129
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:200
icu::number::Notation::NotationUnion::ScientificSettings
Definition: numberformatter.h:341
UCurrencyUsage
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::number::NumberFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberformatter.h:2115
U_MEMORY_ALLOCATION_ERROR
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:457
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:210
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
icu::Format
Base class for all formats.
Definition: format.h:98
fieldpos.h
C++ API: FieldPosition identifies the fields in a formatted output.
unum.h
C API: Compatibility APIs for number formatting.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
icu::number::impl::MacroProps::grouper
Grouper grouper
Definition: numberformatter.h:1381
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:425
icu::number::Notation::NotationUnion::ScientificSettings::fMinExponentDigits
impl::digits_t fMinExponentDigits
Definition: numberformatter.h:347
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:709
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::number::impl::MacroProps::precision
Precision precision
Definition: numberformatter.h:1375
icu::number::Precision::PrecisionUnion::IncrementSettings::fMaxFrac
impl::digits_t fMaxFrac
Definition: numberformatter.h:697
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:265
icu::number::FormattedNumber
The result of a number formatting operation.
Definition: numberformatter.h:2410
icu::number::Notation::NotationUnion::ScientificSettings::fExponentSignDisplay
UNumberSignDisplay fExponentSignDisplay
Definition: numberformatter.h:349
icu::number::UnlocalizedNumberFormatter
A NumberFormatter that does not yet have a locale.
Definition: numberformatter.h:2148
icu::number::impl::Grouper
Definition: numberformatter.h:1216
icu::number::NumberFormatterSettings
An abstract base class for specifying settings related to number formatting.
Definition: numberformatter.h:1442
icu::number::impl::MacroProps::integerWidth
IntegerWidth integerWidth
Definition: numberformatter.h:1387
icu::number::Precision
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.
Definition: numberformatter.h:459
icu::number::impl::MacroProps::padder
Padder padder
Definition: numberformatter.h:1384
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::number::impl::MacroProps::locale
Locale locale
Definition: numberformatter.h:1419
icu::number::impl::MacroProps::notation
Notation notation
Definition: numberformatter.h:1366
dcfmtsym.h
C++ API: Symbols for formatting numbers.
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMaxFrac
impl::digits_t fMaxFrac
Definition: numberformatter.h:683
icu::number::Precision::PrecisionUnion::IncrementSettings::fMinFrac
impl::digits_t fMinFrac
Definition: numberformatter.h:695
icu::number::impl::MacroProps::symbols
SymbolsWrapper symbols
Definition: numberformatter.h:1390
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
appendable.h
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
UNumberUnitWidth
UNumberUnitWidth
An enum declaring how to render units, including currencies.
Definition: unumberformatter.h:98
UNumberDecimalSeparatorDisplay
UNumberDecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
Definition: unumberformatter.h:374
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::number::FractionPrecision
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
Definition: numberformatter.h:778
ucurr.h
C API: Encapsulates information about a currency.
icu::number::CurrencyPrecision
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
Definition: numberformatter.h:834
UNUM_ROUND_HALFEVEN
@ UNUM_ROUND_HALFEVEN
Half-even rounding.
Definition: unum.h:288
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::number::Notation::NotationUnion::ScientificSettings::fRequireMinInt
bool fRequireMinInt
Definition: numberformatter.h:345
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMinSig
impl::digits_t fMinSig
Definition: numberformatter.h:685
icu::number::impl::Grouper::Grouper
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
Definition: numberformatter.h:1231
icu::NumberingSystem
Defines numbering systems.
Definition: numsys.h:60
icu::number::impl::SymbolsWrapper::copyErrorTo
UBool copyErrorTo(UErrorCode &status) const
Definition: numberformatter.h:1186
icu::number::Notation
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
Definition: numberformatter.h:188
icu::number::impl::digits_t
int16_t digits_t
Datatype for minimum/maximum fraction digits.
Definition: numberformatter.h:124
icu::number::ScientificNotation
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
Definition: numberformatter.h:398
UNumberFormatRoundingMode
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition: unum.h:279
icu::StringByteSink
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:235
icu::number::Precision::PrecisionUnion::FractionSignificantSettings
Definition: numberformatter.h:678
icu::number::impl::Padder
Definition: numberformatter.h:1298
icu::number::impl::SymbolsWrapper::SymbolsWrapper
SymbolsWrapper()
Definition: numberformatter.h:1128
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMinFrac
impl::digits_t fMinFrac
Definition: numberformatter.h:681
uobject.h
C++ API: Common ICU base class UObject.
nounit.h
C++ API: units for percent and permille.
plurrule.h
C++ API: PluralRules object.
icu::number::NumberFormatter
See the main description in numberformatter.h for documentation and examples.
Definition: numberformatter.h:2611
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
icu::number::Precision::PrecisionUnion::IncrementSettings
Definition: numberformatter.h:690
UNumberGroupingStrategy
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i....
Definition: unumberformatter.h:193
icu::number::FormattedNumber::FormattedNumber
FormattedNumber()
Default constructor; makes an empty FormattedNumber.
Definition: numberformatter.h:2419
icu::number::impl::SymbolsWrapper
Definition: numberformatter.h:1125
measunit.h
C++ API: A unit for measuring a quantity.
UNumberFormatPadPosition
UNumberFormatPadPosition
The possible number format pad positions.
Definition: unum.h:308
icu::number::Precision::PrecisionUnion::IncrementSettings::fIncrement
double fIncrement
Definition: numberformatter.h:693
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
bytestream.h
C++ API: Interface for writing bytes, and implementation classes.
currunit.h
C++ API: Currency Unit Information.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::number::LocalizedNumberFormatter
A NumberFormatter that has a locale associated with it; this means .format() methods are available.
Definition: numberformatter.h:2228
icu::number::impl::MacroProps
Definition: numberformatter.h:1364
icu::number::impl::MacroProps::copyErrorTo
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Definition: numberformatter.h:1427
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:46
UNumberCompactStyle
UNumberCompactStyle
Constants for specifying short or long format.
Definition: unum.h:319
icu::number::impl::MacroProps::unit
MeasureUnit unit
Definition: numberformatter.h:1369