ICU 62.1  62.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 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBERFORMATTER_H__
8 #define __NUMBERFORMATTER_H__
9 
10 #include "unicode/appendable.h"
11 #include "unicode/dcfmtsym.h"
12 #include "unicode/currunit.h"
13 #include "unicode/fieldpos.h"
14 #include "unicode/fpositer.h"
15 #include "unicode/measunit.h"
16 #include "unicode/nounit.h"
17 #include "unicode/plurrule.h"
18 #include "unicode/ucurr.h"
19 #include "unicode/unum.h"
21 #include "unicode/uobject.h"
22 
23 #ifndef U_HIDE_DRAFT_API
24 
80 
81 // Forward declarations:
82 class IFixedDecimal;
83 class FieldPositionIteratorHandler;
84 
85 namespace numparse {
86 namespace impl {
87 
88 // Forward declarations:
89 class NumberParserImpl;
90 class MultiplierParseHandler;
91 
92 }
93 }
94 
95 namespace number { // icu::number
96 
97 // Forward declarations:
98 class UnlocalizedNumberFormatter;
99 class LocalizedNumberFormatter;
100 class FormattedNumber;
101 class Notation;
102 class ScientificNotation;
103 class Precision;
104 class FractionPrecision;
105 class CurrencyPrecision;
106 class IncrementPrecision;
107 class IntegerWidth;
108 
109 namespace impl {
110 
116 typedef int16_t digits_t;
117 
124 static constexpr int32_t DEFAULT_THRESHOLD = 3;
125 
126 // Forward declarations:
127 class Padder;
128 struct MacroProps;
129 struct MicroProps;
130 class DecimalQuantity;
131 struct UFormattedNumberData;
132 class NumberFormatterImpl;
133 struct ParsedPatternInfo;
134 class ScientificModifier;
135 class MultiplierProducer;
136 class RoundingImpl;
137 class ScientificHandler;
138 class Modifier;
139 class NumberStringBuilder;
140 class AffixPatternProvider;
141 class NumberPropertyMapper;
142 struct DecimalFormatProperties;
143 class MultiplierFormatHandler;
144 class CurrencySymbols;
145 class GeneratorHelpers;
146 class DecNum;
147 
148 } // namespace impl
149 
150 // Reserve extra names in case they are added as classes in the future:
151 typedef Notation CompactNotation;
152 typedef Notation SimpleNotation;
153 
159 class U_I18N_API Notation : public UMemory {
160  public:
185  static ScientificNotation scientific();
186 
209  static ScientificNotation engineering();
210 
252  static CompactNotation compactShort();
253 
276  static CompactNotation compactLong();
277 
302  static SimpleNotation simple();
303 
304  private:
305  enum NotationType {
306  NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
307  } fType;
308 
309  union NotationUnion {
310  // For NTN_SCIENTIFIC
312  int8_t fEngineeringInterval;
313  bool fRequireMinInt;
314  impl::digits_t fMinExponentDigits;
315  UNumberSignDisplay fExponentSignDisplay;
316  } scientific;
317 
318  // For NTN_COMPACT
319  UNumberCompactStyle compactStyle;
320 
321  // For NTN_ERROR
322  UErrorCode errorCode;
323  } fUnion;
324 
326 
327  Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
328 
329  Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
330  fUnion.errorCode = errorCode;
331  }
332 
333  Notation() : fType(NTN_SIMPLE), fUnion() {}
334 
335  UBool copyErrorTo(UErrorCode &status) const {
336  if (fType == NTN_ERROR) {
337  status = fUnion.errorCode;
338  return TRUE;
339  }
340  return FALSE;
341  }
342 
343  // To allow MacroProps to initialize empty instances:
344  friend struct impl::MacroProps;
345  friend class ScientificNotation;
346 
347  // To allow implementation to access internal types:
348  friend class impl::NumberFormatterImpl;
349  friend class impl::ScientificModifier;
350  friend class impl::ScientificHandler;
351 
352  // To allow access to the skeleton generation code:
353  friend class impl::GeneratorHelpers;
354 };
355 
365  public:
379  ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const;
380 
394  ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const;
395 
396  private:
397  // Inherit constructor
398  using Notation::Notation;
399 
400  // Raw constructor for NumberPropertyMapper
401  ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
402  UNumberSignDisplay fExponentSignDisplay);
403 
404  friend class Notation;
405 
406  // So that NumberPropertyMapper can create instances
407  friend class impl::NumberPropertyMapper;
408 };
409 
410 // Reserve extra names in case they are added as classes in the future:
412 
413 // Typedefs for ICU 60/61 compatibility.
414 // These will be removed in ICU 64.
415 // See http://bugs.icu-project.org/trac/ticket/13746
416 typedef Precision Rounder;
420 
429 class U_I18N_API Precision : public UMemory {
430 
431  public:
448  static Precision unlimited();
449 
456  static FractionPrecision integer();
457 
485  static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces);
486 
500  static FractionPrecision minFraction(int32_t minFractionPlaces);
501 
512  static FractionPrecision maxFraction(int32_t maxFractionPlaces);
513 
527  static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces);
528 
542  static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits);
543 
556  static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits);
557 
566  static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits);
567 
579  static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits,
580  int32_t maxSignificantDigits);
581 
582 #ifndef U_HIDE_DEPRECATED_API
583  // Compatiblity methods that will be removed in ICU 64.
584  // See http://bugs.icu-project.org/trac/ticket/13746
585 
587  static inline SignificantDigitsPrecision fixedDigits(int32_t a) {
588  return fixedSignificantDigits(a);
589  }
590 
592  static inline SignificantDigitsPrecision minDigits(int32_t a) {
593  return minSignificantDigits(a);
594  }
595 
597  static inline SignificantDigitsPrecision maxDigits(int32_t a) {
598  return maxSignificantDigits(a);
599  }
600 
602  static inline SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b) {
603  return minMaxSignificantDigits(a, b);
604  }
605 #endif /* U_HIDE_DEPRECATED_API */
606 
626  static IncrementPrecision increment(double roundingIncrement);
627 
645  static CurrencyPrecision currency(UCurrencyUsage currencyUsage);
646 
647 #ifndef U_HIDE_DEPRECATED_API
648 
659  Precision withMode(UNumberFormatRoundingMode roundingMode) const;
660 #endif /* U_HIDE_DEPRECATED_API */
661 
662  private:
663  enum PrecisionType {
664  RND_BOGUS,
665  RND_NONE,
666  RND_FRACTION,
667  RND_SIGNIFICANT,
668  RND_FRACTION_SIGNIFICANT,
669  RND_INCREMENT,
670  RND_CURRENCY,
671  RND_ERROR
672  } fType;
673 
674  union PrecisionUnion {
676  // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
677  impl::digits_t fMinFrac;
678  impl::digits_t fMaxFrac;
679  impl::digits_t fMinSig;
680  impl::digits_t fMaxSig;
681  } fracSig;
683  double fIncrement;
684  impl::digits_t fMinFrac;
685  impl::digits_t fMaxFrac;
686  } increment; // For RND_INCREMENT
687  UCurrencyUsage currencyUsage; // For RND_CURRENCY
688  UErrorCode errorCode; // For RND_ERROR
689  } fUnion;
690 
693 
695  UNumberFormatRoundingMode fRoundingMode;
696 
697  Precision(const PrecisionType& type, const PrecisionUnion& union_,
698  UNumberFormatRoundingMode roundingMode)
699  : fType(type), fUnion(union_), fRoundingMode(roundingMode) {}
700 
701  Precision(UErrorCode errorCode) : fType(RND_ERROR) {
702  fUnion.errorCode = errorCode;
703  }
704 
705  Precision() : fType(RND_BOGUS) {}
706 
707  bool isBogus() const {
708  return fType == RND_BOGUS;
709  }
710 
711  UBool copyErrorTo(UErrorCode &status) const {
712  if (fType == RND_ERROR) {
713  status = fUnion.errorCode;
714  return TRUE;
715  }
716  return FALSE;
717  }
718 
719  // On the parent type so that this method can be called internally on Precision instances.
720  Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
721 
722  static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
723 
724  static Precision constructSignificant(int32_t minSig, int32_t maxSig);
725 
726  static Precision
727  constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig);
728 
729  static IncrementPrecision constructIncrement(double increment, int32_t minFrac);
730 
731  static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
732 
733  static Precision constructPassThrough();
734 
735  // To allow MacroProps/MicroProps to initialize bogus instances:
736  friend struct impl::MacroProps;
737  friend struct impl::MicroProps;
738 
739  // To allow NumberFormatterImpl to access isBogus() and other internal methods:
740  friend class impl::NumberFormatterImpl;
741 
742  // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
743  friend class impl::NumberPropertyMapper;
744 
745  // To allow access to the main implementation class:
746  friend class impl::RoundingImpl;
747 
748  // To allow child classes to call private methods:
749  friend class FractionPrecision;
750  friend class CurrencyPrecision;
751  friend class IncrementPrecision;
752 
753  // To allow access to the skeleton generation code:
754  friend class impl::GeneratorHelpers;
755 };
756 
767  public:
784  Precision withMinDigits(int32_t minSignificantDigits) const;
785 
803  Precision withMaxDigits(int32_t maxSignificantDigits) const;
804 
805  private:
806  // Inherit constructor
807  using Precision::Precision;
808 
809  // To allow parent class to call this class's constructor:
810  friend class Precision;
811 };
812 
823  public:
841  Precision withCurrency(const CurrencyUnit &currency) const;
842 
843  private:
844  // Inherit constructor
845  using Precision::Precision;
846 
847  // To allow parent class to call this class's constructor:
848  friend class Precision;
849 };
850 
861  public:
877  Precision withMinFraction(int32_t minFrac) const;
878 
879  private:
880  // Inherit constructor
881  using Precision::Precision;
882 
883  // To allow parent class to call this class's constructor:
884  friend class Precision;
885 };
886 
897  public:
909  static IntegerWidth zeroFillTo(int32_t minInt);
910 
922  IntegerWidth truncateAt(int32_t maxInt);
923 
924  private:
925  union {
926  struct {
927  impl::digits_t fMinInt;
928  impl::digits_t fMaxInt;
929  bool fFormatFailIfMoreThanMaxDigits;
930  } minMaxInt;
931  UErrorCode errorCode;
932  } fUnion;
933  bool fHasError = false;
934 
935  IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
936 
937  IntegerWidth(UErrorCode errorCode) { // NOLINT
938  fUnion.errorCode = errorCode;
939  fHasError = true;
940  }
941 
942  IntegerWidth() { // NOLINT
943  fUnion.minMaxInt.fMinInt = -1;
944  }
945 
947  static IntegerWidth standard() {
948  return IntegerWidth::zeroFillTo(1);
949  }
950 
951  bool isBogus() const {
952  return !fHasError && fUnion.minMaxInt.fMinInt == -1;
953  }
954 
955  UBool copyErrorTo(UErrorCode &status) const {
956  if (fHasError) {
957  status = fUnion.errorCode;
958  return TRUE;
959  }
960  return FALSE;
961  }
962 
963  void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
964 
965  bool operator==(const IntegerWidth& other) const;
966 
967  // To allow MacroProps/MicroProps to initialize empty instances:
968  friend struct impl::MacroProps;
969  friend struct impl::MicroProps;
970 
971  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
972  friend class impl::NumberFormatterImpl;
973 
974  // So that NumberPropertyMapper can create instances
975  friend class impl::NumberPropertyMapper;
976 
977  // To allow access to the skeleton generation code:
978  friend class impl::GeneratorHelpers;
979 };
980 
989 class U_I18N_API Scale : public UMemory {
990  public:
997  static Scale none();
998 
1009  static Scale powerOfTen(int32_t power);
1010 
1023  static Scale byDecimal(StringPiece multiplicand);
1024 
1033  static Scale byDouble(double multiplicand);
1034 
1041  static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power);
1042 
1043  // We need a custom destructor for the DecNum, which means we need to declare
1044  // the copy/move constructor/assignment quartet.
1045 
1047  Scale(const Scale& other);
1048 
1050  Scale& operator=(const Scale& other);
1051 
1053  Scale(Scale&& src) U_NOEXCEPT;
1054 
1056  Scale& operator=(Scale&& src) U_NOEXCEPT;
1057 
1059  ~Scale();
1060 
1061 #ifndef U_HIDE_INTERNAL_API
1062 
1063  Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1064 #endif /* U_HIDE_INTERNAL_API */
1065 
1066  private:
1067  int32_t fMagnitude;
1068  impl::DecNum* fArbitrary;
1069  UErrorCode fError;
1070 
1071  Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1072 
1073  Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1074 
1075  bool isValid() const {
1076  return fMagnitude != 0 || fArbitrary != nullptr;
1077  }
1078 
1079  UBool copyErrorTo(UErrorCode &status) const {
1080  if (fError != U_ZERO_ERROR) {
1081  status = fError;
1082  return TRUE;
1083  }
1084  return FALSE;
1085  }
1086 
1087  void applyTo(impl::DecimalQuantity& quantity) const;
1088 
1089  void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1090 
1091  // To allow MacroProps/MicroProps to initialize empty instances:
1092  friend struct impl::MacroProps;
1093  friend struct impl::MicroProps;
1094 
1095  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1096  friend class impl::NumberFormatterImpl;
1097 
1098  // To allow the helper class MultiplierFormatHandler access to private fields:
1099  friend class impl::MultiplierFormatHandler;
1100 
1101  // To allow access to the skeleton generation code:
1102  friend class impl::GeneratorHelpers;
1103 
1104  // To allow access to parsing code:
1105  friend class ::icu::numparse::impl::NumberParserImpl;
1106  friend class ::icu::numparse::impl::MultiplierParseHandler;
1107 };
1108 
1109 namespace impl {
1110 
1111 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1114  public:
1116  SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1117 
1119  SymbolsWrapper(const SymbolsWrapper &other);
1120 
1122  SymbolsWrapper &operator=(const SymbolsWrapper &other);
1123 
1125  SymbolsWrapper(SymbolsWrapper&& src) U_NOEXCEPT;
1126 
1128  SymbolsWrapper &operator=(SymbolsWrapper&& src) U_NOEXCEPT;
1129 
1131  ~SymbolsWrapper();
1132 
1133 #ifndef U_HIDE_INTERNAL_API
1134 
1139  void setTo(const DecimalFormatSymbols &dfs);
1140 
1145  void setTo(const NumberingSystem *ns);
1146 
1151  bool isDecimalFormatSymbols() const;
1152 
1157  bool isNumberingSystem() const;
1158 
1163  const DecimalFormatSymbols *getDecimalFormatSymbols() const;
1164 
1169  const NumberingSystem *getNumberingSystem() const;
1170 
1171 #endif // U_HIDE_INTERNAL_API
1172 
1174  UBool copyErrorTo(UErrorCode &status) const {
1175  if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1176  status = U_MEMORY_ALLOCATION_ERROR;
1177  return TRUE;
1178  } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1179  status = U_MEMORY_ALLOCATION_ERROR;
1180  return TRUE;
1181  }
1182  return FALSE;
1183  }
1184 
1185  private:
1186  enum SymbolsPointerType {
1187  SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1188  } fType;
1189 
1190  union {
1191  const DecimalFormatSymbols *dfs;
1192  const NumberingSystem *ns;
1193  } fPtr;
1194 
1195  void doCopyFrom(const SymbolsWrapper &other);
1196 
1197  void doMoveFrom(SymbolsWrapper&& src);
1198 
1199  void doCleanup();
1200 };
1201 
1202 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1204 class U_I18N_API Grouper : public UMemory {
1205  public:
1206 #ifndef U_HIDE_INTERNAL_API
1207 
1208  static Grouper forStrategy(UGroupingStrategy grouping);
1209 
1214  static Grouper forProperties(const DecimalFormatProperties& properties);
1215 
1216  // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1217 
1219  Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UGroupingStrategy strategy)
1220  : fGrouping1(grouping1),
1221  fGrouping2(grouping2),
1222  fMinGrouping(minGrouping),
1223  fStrategy(strategy) {}
1224 #endif // U_HIDE_INTERNAL_API
1225 
1227  int16_t getPrimary() const;
1228 
1230  int16_t getSecondary() const;
1231 
1232  private:
1241  int16_t fGrouping1;
1242  int16_t fGrouping2;
1243 
1251  int16_t fMinGrouping;
1252 
1257  UGroupingStrategy fStrategy;
1258 
1259  Grouper() : fGrouping1(-3) {};
1260 
1261  bool isBogus() const {
1262  return fGrouping1 == -3;
1263  }
1264 
1266  void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1267 
1268  bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1269 
1270  // To allow MacroProps/MicroProps to initialize empty instances:
1271  friend struct MacroProps;
1272  friend struct MicroProps;
1273 
1274  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1275  friend class NumberFormatterImpl;
1276 
1277  // To allow NumberParserImpl to perform setLocaleData():
1278  friend class ::icu::numparse::impl::NumberParserImpl;
1279 
1280  // To allow access to the skeleton generation code:
1281  friend class impl::GeneratorHelpers;
1282 };
1283 
1284 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1286 class U_I18N_API Padder : public UMemory {
1287  public:
1288 #ifndef U_HIDE_INTERNAL_API
1289 
1290  static Padder none();
1291 
1293  static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position);
1294 #endif // U_HIDE_INTERNAL_API
1295 
1297  static Padder forProperties(const DecimalFormatProperties& properties);
1298 
1299  private:
1300  UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1301  union {
1302  struct {
1303  int32_t fCp;
1304  UNumberFormatPadPosition fPosition;
1305  } padding;
1306  UErrorCode errorCode;
1307  } fUnion;
1308 
1309  Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1310 
1311  Padder(int32_t width);
1312 
1313  Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1314  fUnion.errorCode = errorCode;
1315  }
1316 
1317  Padder() : fWidth(-2) {} // NOLINT
1318 
1319  bool isBogus() const {
1320  return fWidth == -2;
1321  }
1322 
1323  UBool copyErrorTo(UErrorCode &status) const {
1324  if (fWidth == -3) {
1325  status = fUnion.errorCode;
1326  return TRUE;
1327  }
1328  return FALSE;
1329  }
1330 
1331  bool isValid() const {
1332  return fWidth > 0;
1333  }
1334 
1335  int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1336  impl::NumberStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1337  UErrorCode &status) const;
1338 
1339  // To allow MacroProps/MicroProps to initialize empty instances:
1340  friend struct MacroProps;
1341  friend struct MicroProps;
1342 
1343  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1344  friend class impl::NumberFormatterImpl;
1345 
1346  // To allow access to the skeleton generation code:
1347  friend class impl::GeneratorHelpers;
1348 };
1349 
1350 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1352 struct U_I18N_API MacroProps : public UMemory {
1355 
1357  MeasureUnit unit; // = NoUnit::base();
1358 
1360  MeasureUnit perUnit; // = NoUnit::base();
1361 
1363  Precision precision; // = Precision(); (bogus)
1364 
1367 
1369  Grouper grouper; // = Grouper(); (bogus)
1370 
1372  Padder padder; // = Padder(); (bogus)
1373 
1375  IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1376 
1379 
1380  // UNUM_XYZ_COUNT denotes null (bogus) values.
1381 
1384 
1387 
1390 
1392  Scale scale; // = Scale(); (benign value)
1393 
1395  const AffixPatternProvider* affixProvider = nullptr; // no ownership
1396 
1398  const PluralRules* rules = nullptr; // no ownership
1399 
1401  const CurrencySymbols* currencySymbols = nullptr; // no ownership
1402 
1404  int32_t threshold = DEFAULT_THRESHOLD;
1405 
1408 
1409  // NOTE: Uses default copy and move constructors.
1410 
1415  bool copyErrorTo(UErrorCode &status) const {
1416  return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1417  padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1418  symbols.copyErrorTo(status) || scale.copyErrorTo(status);
1419  }
1420 };
1421 
1422 } // namespace impl
1423 
1428 template<typename Derived>
1430  public:
1459  Derived notation(const Notation &notation) const &;
1460 
1470  Derived notation(const Notation &notation) &&;
1471 
1516  Derived unit(const icu::MeasureUnit &unit) const &;
1517 
1527  Derived unit(const icu::MeasureUnit &unit) &&;
1528 
1544  Derived adoptUnit(icu::MeasureUnit *unit) const &;
1545 
1555  Derived adoptUnit(icu::MeasureUnit *unit) &&;
1556 
1574  Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1575 
1585  Derived perUnit(const icu::MeasureUnit &perUnit) &&;
1586 
1604  Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &;
1605 
1615  Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&;
1616 
1647  Derived precision(const Precision& precision) const &;
1648 
1658  Derived precision(const Precision& precision) &&;
1659 
1660 #ifndef U_HIDE_DEPRECATED_API
1661  // Compatibility method that will be removed in ICU 64.
1662  // Use precision() instead.
1663  // See http://bugs.icu-project.org/trac/ticket/13746
1665  Derived rounding(const Rounder& rounder) const & {
1666  return precision(rounder);
1667  }
1668 #endif /* U_HIDE_DEPRECATED_API */
1669 
1688  Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &;
1689 
1698  Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&;
1699 
1727  Derived grouping(UGroupingStrategy strategy) const &;
1728 
1739  Derived grouping(UGroupingStrategy 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 
2091  UBool copyErrorTo(UErrorCode &outErrorCode) const {
2092  if (U_FAILURE(outErrorCode)) {
2093  // Do not overwrite the older error code
2094  return TRUE;
2095  }
2096  fMacros.copyErrorTo(outErrorCode);
2097  return U_FAILURE(outErrorCode);
2098  };
2099 
2100  // NOTE: Uses default copy and move constructors.
2101 
2102  protected:
2103  impl::MacroProps fMacros;
2104 
2105  private:
2106  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2107  NumberFormatterSettings() = default;
2108 
2109  friend class LocalizedNumberFormatter;
2110  friend class UnlocalizedNumberFormatter;
2111 };
2112 
2120  : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory {
2121 
2122  public:
2139  LocalizedNumberFormatter locale(const icu::Locale &locale) const &;
2140 
2150  LocalizedNumberFormatter locale(const icu::Locale &locale) &&;
2151 
2157  UnlocalizedNumberFormatter() = default;
2158 
2159  // Make default copy constructor call the NumberFormatterSettings copy constructor.
2165 
2172 
2177  UnlocalizedNumberFormatter& operator=(const UnlocalizedNumberFormatter& other);
2178 
2184  UnlocalizedNumberFormatter& operator=(UnlocalizedNumberFormatter&& src) U_NOEXCEPT;
2185 
2186  private:
2188 
2189  explicit UnlocalizedNumberFormatter(
2191 
2192  // To give the fluent setters access to this class's constructor:
2193  friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2194 
2195  // To give NumberFormatter::with() access to this class's constructor:
2196  friend class NumberFormatter;
2197 };
2198 
2206  : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory {
2207  public:
2219  FormattedNumber formatInt(int64_t value, UErrorCode &status) const;
2220 
2232  FormattedNumber formatDouble(double value, UErrorCode &status) const;
2233 
2248  FormattedNumber formatDecimal(StringPiece value, UErrorCode& status) const;
2249 
2250 #ifndef U_HIDE_INTERNAL_API
2251 
2255  FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2256 
2260  void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const;
2261 
2266  const impl::NumberFormatterImpl* getCompiled() const;
2267 
2272  int32_t getCallCount() const;
2273 
2274 #endif
2275 
2289  Format* toFormat(UErrorCode& status) const;
2290 
2296  LocalizedNumberFormatter() = default;
2297 
2298  // Make default copy constructor call the NumberFormatterSettings copy constructor.
2304 
2311 
2316  LocalizedNumberFormatter& operator=(const LocalizedNumberFormatter& other);
2317 
2323  LocalizedNumberFormatter& operator=(LocalizedNumberFormatter&& src) U_NOEXCEPT;
2324 
2325 #ifndef U_HIDE_INTERNAL_API
2326 
2338  void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2339 
2340 #endif
2341 
2347 
2348  private:
2349  // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2350  // header, and LocalPointer needs the full class definition in order to delete the instance.
2351  const impl::NumberFormatterImpl* fCompiled {nullptr};
2352  char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2353 
2355 
2357 
2358  LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2359 
2360  LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2361 
2362  void lnfMoveHelper(LocalizedNumberFormatter&& src);
2363 
2367  bool computeCompiled(UErrorCode& status) const;
2368 
2369  // To give the fluent setters access to this class's constructor:
2370  friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2371  friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2372 
2373  // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2374  friend class UnlocalizedNumberFormatter;
2375 };
2376 
2384  public:
2385 #ifndef U_HIDE_DEPRECATED_API
2386 
2394  UnicodeString toString() const;
2395 #endif /* U_HIDE_DEPRECATED_API */
2396 
2405  UnicodeString toString(UErrorCode& status) const;
2406 
2407 #ifndef U_HIDE_DEPRECATED_API
2408 
2419  Appendable &appendTo(Appendable &appendable);
2420 #endif /* U_HIDE_DEPRECATED_API */
2421 
2433  Appendable &appendTo(Appendable &appendable, UErrorCode& status);
2434 
2435 #ifndef U_HIDE_DEPRECATED_API
2436 
2456  void populateFieldPosition(FieldPosition &fieldPosition, UErrorCode &status);
2457 #endif /* U_HIDE_DEPRECATED_API */
2458 
2490  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
2491 
2492 #ifndef U_HIDE_DEPRECATED_API
2493 
2508  void populateFieldPositionIterator(FieldPositionIterator &iterator, UErrorCode &status);
2509 #endif /* U_HIDE_DEPRECATED_API */
2510 
2524  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
2525 
2526 #ifndef U_HIDE_INTERNAL_API
2527 
2532  void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
2533 
2538  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
2539 
2540 #endif
2541 
2545  FormattedNumber(const FormattedNumber&) = delete;
2546 
2550  FormattedNumber& operator=(const FormattedNumber&) = delete;
2551 
2557  FormattedNumber(FormattedNumber&& src) U_NOEXCEPT;
2558 
2564  FormattedNumber& operator=(FormattedNumber&& src) U_NOEXCEPT;
2565 
2570  ~FormattedNumber();
2571 
2572  private:
2573  // Can't use LocalPointer because UFormattedNumberData is forward-declared
2574  const impl::UFormattedNumberData *fResults;
2575 
2576  // Error code for the terminal methods
2577  UErrorCode fErrorCode;
2578 
2583  explicit FormattedNumber(impl::UFormattedNumberData *results)
2584  : fResults(results), fErrorCode(U_ZERO_ERROR) {};
2585 
2586  explicit FormattedNumber(UErrorCode errorCode)
2587  : fResults(nullptr), fErrorCode(errorCode) {};
2588 
2589  // To give LocalizedNumberFormatter format methods access to this class's constructor:
2590  friend class LocalizedNumberFormatter;
2591 };
2592 
2599  public:
2607  static UnlocalizedNumberFormatter with();
2608 
2618  static LocalizedNumberFormatter withLocale(const Locale &locale);
2619 
2631  static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, UErrorCode& status);
2632 
2636  NumberFormatter() = delete;
2637 };
2638 
2639 } // namespace number
2641 
2642 #endif // U_HIDE_DRAFT_API
2643 
2644 #endif // __NUMBERFORMATTER_H__
2645 
2646 #endif /* #if !UCONFIG_NO_FORMATTING */
One more than the highest UNumberSignDisplay value.
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
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition: unum.h:279
static SignificantDigitsPrecision minDigits(int32_t a)
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:38
C++ API: Currency Unit Information.
C-compatible API for localized number formatting; not recommended for C++.
static SignificantDigitsPrecision fixedDigits(int32_t a)
See the main description in numberformatter.h for documentation and examples.
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:694
A class that defines the strategy for padding and truncating integers before the decimal separator...
C++ API: FieldPosition Iterator.
static SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b)
UGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten).
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UGroupingStrategy strategy)
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
Defines numbering systems.
Definition: numsys.h:60
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
C++ API: units for percent and permille.
static SignificantDigitsPrecision maxDigits(int32_t a)
No error, no warning.
Definition: utypes.h:430
C++ API: PluralRules object.
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
UNumberDecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:196
Memory allocation error.
Definition: utypes.h:438
An abstract base class for specifying settings related to number formatting.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
Half-even rounding.
Definition: unum.h:288
C++ API: FieldPosition identifies the fields in a formatted output.
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
C++ API: A unit for measuring a quantity.
C API: Encapsulates information about a currency.
UNumberSignDisplay
An enum declaring how to denote positive and negative numbers.
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:400
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:240
A unit of currency, such as USD (U.S.
Definition: currunit.h:37
UNumberFormatPadPosition
The possible number format pad positions.
Definition: unum.h:308
C++ API: Common ICU base class UObject.
A NumberFormatter that does not yet have a locale.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UNumberCompactStyle
Constants for specifying short or long format.
Definition: unum.h:319
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
Derived rounding(const Rounder &rounder) const &
A class that defines a quantity by which a number should be multiplied when formatting.
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
C++ API: Symbols for formatting numbers.
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
UNumberUnitWidth
An enum declaring how to render units, including currencies.
Basic definitions for ICU, for both C and C++ APIs.
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:244
int16_t digits_t
Datatype for minimum/maximum fraction digits.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
The result of a number formatting operation.
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
UMemory is the common ICU base class.
Definition: uobject.h:112
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter...
One more than the highest UNumberDecimalSeparatorDisplay value.
One more than the highest UNumberUnitWidth value.
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
C API: Compatibility APIs for number formatting.
UBool copyErrorTo(UErrorCode &status) const
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188